...

View Full Version : mysql insert error



speedy_rudolf
09-24-2010, 03:51 PM
Hi. I think you all know me by now so I'll cut to the chase.

<?php
$host="edited";
$username="edited";
$password="edited";
$db_name="edited";
$tbl_name="topic";
// Connect to server and select databse.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

$name=$_POST['name'];
$detail=$_POST['details'];

$sql="INSERT INTO $tbl_name(topic, detail, datetime)VALUES('$name', '$detail', NOW())";
$result=mysql_query($sql);

if($result){
header("location:site.html");}
else{
echo("I have failed you master.");}
?>

Displayed error: "I have failed you master." Anyone know a possible cause? Thanks. Bye.

Spudhead
09-24-2010, 04:00 PM
Instead of "I have failed you master", get the PHP to echo the $sql string that you're sending to the database.

As an aside, dropping unsanitised input straight into a database leaves you wide open to a SQL injection attack. At the very least, wrap $name and $detail in mysql_real_escape_string() (http://php.net/manual/en/function.mysql-real-escape-string.php),

abduraooft
09-24-2010, 04:05 PM
$result=mysql_query($sql) or die(mysql_error());

it's your responsibility to die(), if necessary!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum