Call to undefined function last_insert_id() [duplicate] - php

This question already has answers here:
How do I get the last inserted ID of a MySQL table in PHP?
(16 answers)
Closed 9 years ago.
I would like to return the ID "setsId" after I insert values in the the Sets table.
This is the error I receive: Call to undefined function last_insert_id().
Here is my code:
if($_POST[reps]=="")
{
echo "Record can not be added. All fields are mandatory";
echo '<br>';
}
else
{
$con = mysqli_connect(); //removed for privacy
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO Sets (exerciseId, bandId, reps, notes)
VALUES ('$_POST[exerciseId]',
'$_POST[bandId]',
'$_POST[reps]',
'$_POST[notes]')";
$sId=last_insert_id();
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo '<div id ="error">';
echo 'SUCCESS!';
echo '<br>';
echo "1 record added";
echo '<br>';
echo '</div>';
mysqli_close($con);
}
include ('ptfooter.html');
?>
Could someone please let me know how I am using the function last_insert_id() incorrectly so that it would cause an error?

last_insert_id is the name of a MySQL function. The corresponding mysqli function is called mysqli_insert_id. Use that instead.
But you can't call that function if you haven't executed your insert query yet. Make sure that your insert is executed (and succeeded) before you call that function.

Related

insert data to database with php [duplicate]

This question already has an answer here:
Error No Database Selected PHP/mySQL
(1 answer)
Closed 6 years ago.
I want to insert a data in database by this php code:
<?php
/*if(isset($_POST['username'])&&isset($_POST['password'])) {
$username = $_POST['username'];
$password = $_POST['password'];*/
$DBS="localhost";// Database server
$DBU="pb";
$DBP="******";
$con= new mysqli($DBS,$DBU,$DBP);
if($con->connect_error)
die("Connection failed: " . $con->connect_error);
else{
echo "Connected Sucssefully!";
$sql = "INSERT INTO users (username, password, name)
VALUES ('JJ', 'Doe', 'John')";
if($con->query($sql)===TRUE)
echo "User Created";
else
echo "Not Created!";}
//}
?>
but I get "Connected Sucssefully!Not Created!"! Idon't know why!
Thank you ! Specially #Arnolio . I used $con->error and it gave me "No database selected " and this error solved my problem ! I did not specify any database name!
Regaards

Mysqli statement returning warnings [duplicate]

This question already has an answer here:
mysqli_real_escape_string() expects exactly 2 parameters, 1 given Fatal Error [duplicate]
(1 answer)
Closed 6 years ago.
Here is my code:
include "db_conx.php";
$sql = mysqli_query("INSERT INTO table(column) VALUES('" . mysqli_real_escape_string($var) . "')");
if ($sql) {echo "connection successful";
} else {
echo "failure";
}
It returns these errors:
Warning: mysqli_real_escape_string() expects exactly 2 parameters, 1 given
Warning: mysqli_query() expects at least 2 parameters, 1 given
I tried using PDO but that didn't work either...
First Parameter is mysql connection link identifier, and second is string For more details, you can visit this link : http://in2.php.net/manual/en/mysqli.real-escape-string.php.
include "db_conx.php";
$sql = mysqli_query(pass_your_connection_identifier_here ,"INSERT INTO table(column) VALUES('" . mysqli_real_escape_string(pass_your_connection_identifier_here, $var) . "')");
if ($sql) {echo "connection successful";
} else {
echo "failure";
}
You are missing connection identifier in both mysqli_real_escape_string() and mysqli_query()
change your code to mysqli_query($connection,$sql) and mysqli_real_escape_string($connection,$string)
You are missing connection variable at both the lines that's why you are facing issues :
Try to replace your code with this :
//$con // it is your connection variable
include "db_conx.php";
$sql = mysqli_query($con,"INSERT INTO table(column) VALUES('" . mysqli_real_escape_string($con,$var) . "')");
if ($sql) {echo "connection successful";
} else {
echo "failure";
}

PHP sending a post [duplicate]

This question already has an answer here:
Syntax error due to using a reserved word as a table or column name in MySQL
(1 answer)
Closed 7 years ago.
I am currently trying to use PHP as a backend and MYSQL as my database to setup a simple PHP script that will send a friend request.
There are two parameters for a friend request in my MYSQL data base, From, Too. The Database name is send_friendreq and the table in that database is pending_req.
I have tried multiple ways of sending a post, including PostMan and a different addon but everytime I send the post, I get an error from my PHP code which is "Failed". From my understanding this means that it is connecting to the database fine, but it's not actually sending the data too the Database.
I'm not sure if I have the database set up wrong, or if my PHP is wrong but any help would be extrememly appreciated.
Here is my code for the PHP backend
//Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_errno();
}
if (isset($_POST['Username']) && isset($_POST['FriendReq']))
{
$username = $_POST['Username'];
$usernamebeingreq = $_POST['FriendReq'];
//$sqlCheck = "SELECT Username FROM Users WHERE Username = '" . $usernamebeingreq . "'";
//$resultCheck = mysqli_query($con, $sqlCheck);
//if(!$resultCheck)
//{
//echo "Invalid Username";
//}
//else
//{
$sql="INSERT INTO pending_req (To, From) VALUES ('$usernamebeingreq', '$username')";
$result = mysqli_query($con, $sql);
if(!$result)
{
echo 'Failed';
}
else
{
echo 'Friend added!';
}
//}
}
else
{
echo 'Missing Parameters';
}
?>
If you are in need of my database information, I can reveal that!
from and to are reserved words in SQL you have to add backticks arrond:
$sql="INSERT INTO pending_req (`To`, `From`) VALUES ('$usernamebeingreq', '$username')";
or better rename the column.
Hint: use prepared statement. it is much more safty.

mysql php displays results but not inserting into database

I stripped down query for only one insert
<?php
session_start();
include 'cstring.php';
$title="";
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
else {
$title=$_POST['title'];
$query=mysqli_query($con,"insert into blogpages(blogpagetitle) values('".$title."')");
if($query){
$bloga="sucessfully added a new blog";
echo $bloga;
}
else {
echo mysqli_error($con); // if using mysqli do not use mysql in between
}
}
mysqli_close($con);
?>
is there something wong in this code that it doesnt insert into mysql
table structure
1.bpid int(50)--------------null-no default-none autoincrement
2.blogpagetitle------------varchar(255) utf16_general_ci
3.datemade-------------timestamp current time stamp
4.blogpagedescription---------text utf16_general_ci
5.blogbody----------------longtext utf16_general_ci
6.blogpageextended------------ text utf16_general_ci
TIP
Sanitize variables, Use mysqli_real_escape_string()
When you are not able to debug your code, echo every possible stuff and die the rest code.
For example here, echo if there is error in DB connection, echo the query to see if it is correct, echo the result of query execution, echo if there is some error!
You should be using echo mysqli_error($con) to get the error message rather than mysql_error().

How to debug mysqli_query? [duplicate]

This question already has an answer here:
What to do with mysqli problems? Errors like mysqli_fetch_array(): Argument #1 must be of type mysqli_result and such
(1 answer)
Closed 8 years ago.
I have a connection to my database:
$con=mysqli_connect("localhost","xxxx","xxxxx","xxxxxx");
// Check connection
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
which returns no error.
I then try to perform a mysqli_query:
$result = mysqli_query($con,"INSERT INTO entries (name, genre, info , date , website , twebsite, video , tvideo, image, extra_genre)
VALUES ('".$name."', '".$type."', '".$info."', '".$date."', '".$url."', '".$href."', '".$_POST["video"]."', '".$videotype."', 'video images/".$photo."', '".$type2."')");
with the alert whether this insertion to the database has worked or not:
if($result)
{
echo "Success:";
}
else
{
echo "error inserting data into database: ";
}
When I perform my code I get the output error inserting data into database:
But I have no ides why $result is not successful?
How can I debug it?
You are looking for mysqli_error() function.
Problem here is that date is a reserved word, so you have to escape that: `date`
try this:
echo $result;
and then run the sql output.. that should give you an error message.

Categories