PDA

View Full Version : Php help!


pudsey
18-04-10, 07:25 AM
This is some php code for a Uni project! It dont work! Can anyone see any errors! Cheers! :confused:




<?php

$connect = mysql_connect("localhost","root","live@49");
$db = mysql_select_db("flashphptask_zxq_customers", $connect);

$sql = mysql_query("INSERT INTO comments (ID, Name, eMail, Comments)
VALUE ('$ID', '$Name','$eMail','$Comments')")or die
(mysql_error());

?>

Kalessin
18-04-10, 10:17 AM
What error are you getting?

pudsey
18-04-10, 10:18 AM
There is no error - it just wont write anything to the database I have set up! been annoying me all night long!

christopher
18-04-10, 10:34 AM
Assuming you have given a value to the variables you're using within the query then it will work... you have given the variables a value haven't you? I.e.

$ID = 'This goes in the ID column';
$Name = 'And this in the Name column';

Wester
18-04-10, 11:11 AM
I think something slightly worse is awry if you are not getting any error messages at all, but you could try these suggestions.

Why are you inserting an ID? is that the id for your item that will be incremented each time you add a new item? if so you should have it set as an auto_increment column in mysql, Then you wont need to insert it manually each time.

What is the column type for name, email and comments? If they are set to text you need to be adding them in the SQL query as such:

("INSERT INTO comments (ID, Name, eMail, Comments)
VALUE ('$ID', \''$Name'\',\''$eMail'\',\''$Comments'\')")

Hope that helps.

Kalessin
18-04-10, 11:24 AM
I think something slightly worse is awry if you are not getting any error messages at all

Not necessarily. If using an integrated LAMP stack such as XAMPP or MAMP, PHP errors may be written to an error log rather than displayed on screen.

Pudsey, check your error logs!

SoulKiss
18-04-10, 11:41 AM
Not necessarily. If using an integrated LAMP stack such as XAMPP or MAMP, PHP errors may be written to an error log rather than displayed on screen.

Pudsey, check your error logs!

Check your mySQL logs too, could be a permissions thing on the database.

Can you log into mySQL using those credentials and do an insert?

Kalessin
18-04-10, 11:47 AM
Also, do you have phpMyAdmin installed? If so, is it working?

GeneticBubble
18-04-10, 12:14 PM
This is some php code for a Uni project! It dont work! Can anyone see any errors! Cheers! :confused:




<?php

$connect = mysql_connect("localhost","root","live@49");
$db = mysql_select_db("flashphptask_zxq_customers", $connect);

$sql = mysql_query("INSERT INTO comments (ID, Name, eMail, Comments)
VALUE ('$ID', '$Name','$eMail','$Comments')")or die
(mysql_error());

?>



what is this...i dont even...:confused:

TSM
18-04-10, 12:24 PM
$con = mysql_connect("localhost","root","live@49");
if(!$con){
die("Error conn - ".mysql_error());
}else{
$db = mysql_select_db("flashphptask_zxq_customers", $con);
if(!$db){
die("Error changedb - ".mysql_error());
}else{
$sql = mysql_query("INSERT INTO comments (ID, Name, eMail, Comments) VALUE ('$ID', '$Name','$eMail','$Comments')",$db)or die
(mysql_error());
}
}


Saftey points...
make sure you use mysql_real_escape_string() on your insert values, dont forget the ; at the end as an additional saftey, mabey wrap your query up with a sprintf such as...





$con = mysql_connect("localhost","root","live@49");
if(!$con){
die("Error conn - ".mysql_error());
}else{
$db = mysql_select_db("flashphptask_zxq_customers", $con);
if(!$db){
die("Error changedb - ".mysql_error());
}else{
$sqlstring=sprintf("INSERT INTO comments (ID, Name, eMail, Comments) VALUE ('%u', '%s',%s','%s');",
$ID,
mysql_real_escape_string($Name),
mysql_real_escape_string($eMail),
mysql_real_escape_string($Comments));
$sql = mysql_query($sqlstring,$db)or die ("Error query - ".mysql_error());
}
}


Its not OOP but should work fine if you just want a simple connection and query.
You could just use PDO or get a DB Class to do most of it for you in OOP standard.

TSM
18-04-10, 12:30 PM
Just going back on a few things that people pointed out here, if $ID is an autoincremental field, dont include it in the INSERT statement.
Also we assume you have set all the variables required above the script that we cant see.

keith_d
18-04-10, 03:58 PM
Try putting the SQL command into a variable.. I usually do it this way:

$sql='INSERT INTO t_mytable .....';
$resultset=mysql_query($sql) or die((mysql_error());

That way it's easy to echo/print/html the contents of $sql when things go wrong. If you can't see what's wrong there's even the option to cut and paste into mySQL and run it directly against the database.

Just my thoughts,

Keith.