I’m trying to insert data into my table with a foreign key that has auto-increment. Inserting into the main puzzle table is fine but I need the NULL in the insert. The problem is when I try to insert it into the records table with last_insert_id(). It doesn’t add anything to the table. What am I doing wrong?
This is the code:
<html>
<head>
<title>Insert Puzzles Results</title>
</head>
<body>
<?
// get the data from the form and assign the data to variables
$name = $_POST['name'];
$type = $_POST['type'];
$description = $_POST['description'];
$ability = $_POST['ability'];
$time = $_POST['time'];
// check to see if all the data is there
if (!$name
|| !$type
|| !$description
|| !$ability
|| !$time)
{
echo "You have not entered all the required details.<br>"
."Please go back and try again.";
exit;
}
// add slashes and prepare the data for inserting into the db
$name = addslashes($name);
$type = addslashes($type);
$description = addslashes($description);
$ability = addslashes($ability);
$time = addslashes($time);
// connect to the db
@ $db = mysql_pconnect("sql300.epizy.com","epiz_33978615","password");
if (!$db)
{
echo "ERROR: Could not connect to database. Please try again later.";
exit;
}
// select the db
mysql_select_db("epiz_33978615_week12");
// prepare the query
$query = "insert into puzzles values
('".NULL."','".$name."','".$type."','".$description."')";
// run the query
$result = mysql_query($query);
if($result)
echo mysql_affected_rows()." puzzle added to Database.<br>";
$query = "insert into records values
('".'"last_insert_id()"'."','".$ability."','".$time."')";
$result = mysql_query($query);
if($result)
echo mysql_affected_rows()." time added to Database.<br>";
?>
</body>
</html>
I tried to update the code and got to this. It runs but doesn’t do anything and doesn’t show any errors.
<html>
<head>
<title>Insert Puzzles Results</title>
</head>
<body>
<?
// get the data from the form and assign the data to variables
$name = $_POST['name'];
$type = $_POST['type'];
$description = $_POST['description'];
$ability = $_POST['ability'];
$time = $_POST['time'];
// check to see if all the data is there
if (!$name
|| !$type
|| !$description
|| !$ability
|| !$time)
{
echo "You have not entered all the required details.<br>"
."Please go back and try again.";
exit;
}
// add slashes and prepare the data for inserting into the db
$name = addslashes($name);
$type = addslashes($type);
$description = addslashes($description);
$ability = addslashes($ability);
$time = addslashes($time);
// connect to the db
$conn = mysqli_connect("sql300.epizy.com","epiz_33978615","password","epiz_33978615_week12");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// prepare the query
$query = "INSERT INTO puzzles (number, name, type, description)
VALUES ('$name', '$type', '$description')";
// run the query
$result = mysqli_query($conn, $query);
if($result)
echo mysqli_affected_rows()." puzzle added to Database.<br>";
$query = "INSERT INTO records (number, ability, time)
VALUES ((LAST_INSERT_ID(), NULL), '$ability', '$time')";
$result = mysqli_query($conn, $query);
if($result)
echo mysqli_affected_rows()." time added to Database.<br>";
?>
</body>
</html>
I tried fixing the problem of the query and affected rows but now Im back at the beginning.
<html>
<head>
<title>Insert Puzzles Results</title>
</head>
<body>
<?
// get the data from the form and assign the data to variables
$name = $_POST['name'];
$type = $_POST['type'];
$description = $_POST['description'];
$ability = $_POST['ability'];
$time = $_POST['time'];
// check to see if all the data is there
if (!$name
|| !$type
|| !$description
|| !$ability
|| !$time)
{
echo "You have not entered all the required details.<br>"
."Please go back and try again.";
exit;
}
// add slashes and prepare the data for inserting into the db
$name = addslashes($name);
$type = addslashes($type);
$description = addslashes($description);
$ability = addslashes($ability);
$time = addslashes($time);
// connect to the db
$conn = mysqli_connect("sql300.epizy.com","epiz_33978615","-REMOVEDMYMOD-","epiz_33978615_week12");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// prepare the query
mysqli_query($conn, "INSERT INTO puzzles (name, type, description)
VALUES ('$name', '$type', '$description')");
echo mysqli_affected_rows($conn)." puzzle added to Database.<br>";
mysqli_query($conn, "INSERT INTO records (number, ability, time)
VALUES ('(mysqli_insert_id($conn), NULL)', '$ability', '$time')");
echo mysqli_affected_rows($conn)." time added to Database.<br>";
?>
</body>
</html>
The puzzle table will be updated but the records table has this error.
Fatal error : Uncaught Error: Object of class mysqli could not be converted to string in /home/vol1_5/epizy.com/epiz_33978615/htdocs/put_puzzle4.php:59 Stack trace: #0 {main} thrown in /home/vol1_5/epizy.com/epiz_33978615/htdocs/put_puzzle4.php on line 59