PHP inserting into MYSQL

I have some issues with assigning POST values directly to variables.

It appears you are not using $sql ?

Are you certain all of the INTO - VALUES pair up?

1 Like

I was thinking the same thing. I counted 4 data types, but there’s 5 columns specified.

2 Likes

I looked a bit for documentation on how PHP deals with sequential executes.

In particular, whether or not the second might not complete before the first.

1 Like

Hi my select statement is $stmts = $conn->prepare($sqls);
$stmts->bind_param(“sss”, $f,$l,$e);
$result = $stmts->execute();
$data = $result->fetch_array(MYSQLI_BOTH);

and currently getting
Uncaught Error: Call to a member function fetch_array() on boolean in …32 Stack trace: #0 {main} thrown in

That’s because

Is not part of prepared statements. Here’s the right one.

http://php.net/manual/en/mysqli-stmt.fetch.php

1 Like

Are you sure you have “clientid” in table “clienttb” ?

Because it select clientid from cliettb and with that you getting id for messagetb.

If you want to use PDO with prepared statements try like this first

<?php

error_reporting(E_ALL);

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "mycms";

// Create connection
$db = new PDO('mysql:host='.$servername.';dbname='.$dbname, $username, $password);
// Check connection
if (!$db) {
    die("Can't connect to database!");
};

// if submit button is pressed
if (isset($_POST['submit'])) {

	// get values from post
	$f = $_POST['fname'];
	$l = $_POST['lname'];
	$e = $_POST['email'];
	$t = $_POST['telephone'];
	$m = $_POST['message'];

	// check if user with same first name, last name, email already exists
	$stmt = $db->prepare("SELECT clientid, firstName, lastName, email FROM clienttb WHERE firstName = :firstname AND lastName = :lastname AND email = :email");
	$stmt->bindParam(':firstname', $f);
	$stmt->bindParam(':lastname', $l);
	$stmt->bindParam(':email', $e);
	$stmt->execute();
	$result = $stmt->fetch();

	// if user with first name, last name, email exists
	if ($result) {
		// user with that data exists !
		var_dump($result);
	} else {
		// user with that data don't exists !
		echo "No result";
	}

}

?>

Updated my code to

<?php error_reporting(E_ALL); $servername = "localhost"; $username = "root"; $password = "admin"; $dbname = "adrian"; // Create connection $conn = new PDO('mysql:host='.$servername.';dbname='.$dbname, $username, $password); // Check connection if (!$conn) { die("Can't connect to database!"); }; // escape this post values ! $f = $_POST['fname']; $l = $_POST['lname']; $e = $_POST['email']; $t = $_POST['telephone']; $m = $_POST['message']; $stmt = $conn->prepare("SELECT clientid, firstName, lastName, email FROM clienttb WHERE firstName = :firstname AND lastName = :lastname AND email = :email"); $stmt->bindParam(':firstname', $f); $stmt->bindParam(':lastname', $l); $stmt->bindParam(':email', $e); $stmt->execute(); $result = $stmt->fetch(); $c = $result['clientid']; $sqlm = "INSERT INTO messagetb(message, clientid, messageDate) VALUES (message = :msg , clientid = :cid , CURRENT_TIMESTAMP())"; $stmtm = $conn->prepare($sqlm); $stmtm->bindparam(':msg',$m); $stmtm->bindparam(':cid',$c); $sqlc = "INSERT INTO clienttb(firstName, lastName, email, telephone, dateCreated) VALUES (firstName =:fn , lastName=:ln, email =:em, telephone = :tel, CURRENT_TIMESTAMP())"; $stmtc = $conn->prepare($sqlc); $stmtc->bindparam(':fn',$f); $stmtc->bindparam(':ln',$l); $stmtc->bindparam(':em',$e); $stmtc->bindparam(':tel',$t); // if returned rows count more that 0, insert data // put both querys here if ($result) { $query = $stmtm->execute(); } else{ $query = $stmtc->execute(); $query = $stmtm->execute(); } if ($query) { echo "New records created successfully"; } else { echo "Error:
" . $conn->errorinfo; } ?>

now get
Notice: Undefined property: PDO::$errorinfo in C:\xampp\htdocs\dataConnection.php on line 60
Error:

Looks like something wrong with the queries.
Only entering blank ‘clientid’

Can you explain this ? “to insert a message based on if the first and last name and email match”

What if any of this is not match ? What then script need to do ?

If you entered data from user that exists in database what did you get with var_dump($results); ?

Don’t just copy paste code, test it first piece by piece.

I guess you are missing clientid from clienttb table.

Did you try to echo $c; to see if you getting clientid ?

To explain. its to reduce duplicates of client information. So if the first and last names and email matchthen just create a message with the clientid

The error im seeing is in mysql. error 1452 cannot add or update child row.
foreign key and reference both clientid

Its all working thanks for your help

Both of these are incorrect INSERT queries.

This message is obvious. The reason why I like PHP’s error messages is because they tell you the obvious. They point out the obvious. It takes very little to understand what the error message is telling you.

It’s pointing to that line. Saying that there is no such property called errorinfo which pretty much says that that property doesn’t exist or is a typo. Which also means that you are using the wrong syntax. You should also not be outputting errors to the screen rather, send it to your error logs so you can take a look at them.

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.