Question:
php mysql trying to send data to data base but getting Error, insert query failed?
ganger
2010-05-22 23:21:13 UTC
am trying to pass $simon into myaql database but am getting the insert query failed it is connecting to data base but when i try to pass a value into the database get the error insert query failed am i use the wrong code for insert the data into my database

session_start();
$_SESSION['username']==$myusername;
if (($_SESSION['auth'] == false)||(!isset($_SESSION['auth'])))
{
header("location:login.html");
}
$host="localhost"; // Host name
$username="20203800"; // Mysql username
$password="ralph"; // Mysql password
$db_name="two0203800"; // Database name
$tbl_name="link"; // Table name


mysql_connect("$host", "$username", "$password")or die("cannot connect");

$query = "INSERT INTO $tbl_name (username)
VALUES ( '$simon' )";

mysql_query($query)
or die('Error, insert query failed'.mysql_error());


mysql_close($con);
// header( 'Location:http://2020.sost.blackburn.ac.uk/members.html ' ) ;

?>
Three answers:
Ray
2010-05-23 06:04:41 UTC
You didn't select the database, and you are also trying to close a connection that isn't open.



Change your connect statement to



$con=@mysql_connect("$host", "$username", "$password")or die("cannot connect");



Then you can select the database with



$db=@mysql_select_db($db_name, $con);
Dasun Costa
2010-05-22 23:45:21 UTC
After connecting to the server, you should select the database. You can do this by,

$dbs=mysql_select_db('two0203800')or die('cannot use db');
Barbara
2016-04-12 05:38:41 UTC
Well first of all, the setcookie is invalid. You need to close the parenthesis and all that. I have modified the code for it to work.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...