PHP query using an association from another query

Multi tool use
Multi tool use


PHP query using an association from another query



I am trying to INSERT into my SQL table in PHP using an association I already returned from a previous SELECT query. But the compiler is giving me a syntax error 'unexpected $EOF'.



Essentially, this is what I am doing:


$query2 = "SELECT * FROM users WHERE user_name='$user.username'";
$result2 = mysqli_query($db, $query2);
$new_user = mysqli_fetch_assoc($result2);

$query3 = "INSERT INTO new_table (new_user_id) VALUES('$new_user['user_id']');";
mysqli_query($db, $query3);



Query 3 is the one giving me the error. I have referenced variables like this before with the fetch association. I'm incredibly new to PHP, but isn't that fetch by association just creating a dictionary? Any ideas?



Thanks,





Can you post the output of $new_user['user_id']?
– Steve Mulvihill
Jul 1 at 2:30





@SteveMulvihill $new_user['user_id'] returns my correct ID. I don't get errors if I do $new_user.user_id. Is that a way to reference it?
– slnd54
Jul 1 at 2:35





@user3783243 what do you mean?
– slnd54
Jul 1 at 2:37





This would not cause the unexpected EOF but you have some issues here. $user.username -- is that supposed to be an object property? PHP does not understand that syntax. $user->username or if an array $user['username'] but dot notation is not part of PHP. Later you must encluse $new_user['user_id'] in {} as {$new_user['user_id']} to use it in a single quoted string while also quoting the array key. See examples of PHP string double quoting secure.php.net/manual/en/…
– Michael Berkowski
Jul 1 at 2:41


$user.username


$user->username


$user['username']


$new_user['user_id']


{}


{$new_user['user_id']}





@MichaelBerkowski I appreciate that, I will give it a read.
– slnd54
Jul 1 at 2:43




2 Answers
2



Looks like the issue is with the single quote marks in the code below:


VALUES('$new_user['user_id']')



If you were to set a variable outside of the query like this it should work:


$user_id = $new_user['user_id'];
$query3 = "INSERT INTO new_table (new_user_id) VALUES('$user_id');";



I would also suggest you parameterize your queries.





But I could also do {$new_user['user_id']} as well?
– slnd54
Jul 1 at 2:47





That should work as well.
– Steve Mulvihill
Jul 1 at 2:49



The EOF error is short for end of file which means the PHP doesn't think your script ends where you think it does. This is usually caused by a control block not being closed e.g. {...code ?> with no closing }.


EOF


end of file


{...code ?>


}



You have some other issues that should be address. You shouldn't use variables in queries. You can't use a quoted index in a string. You don't need to execute 2 queries to do what you are trying to do.


INSERT INTO new_table (new_user_id) SELECT user_id FROM users WHERE user_name= ?



Should be work. Use that with prepare, bind_param, and execute and your code will be much more secure.


prepare


bind_param


execute



You also can read more about these concepts here, http://php.net/manual/en/mysqli.quickstart.prepared-statements.php.



Also if interested in how that SQL query works see this https://dev.mysql.com/doc/refman/8.0/en/insert-select.html.





Thank you, I will read these. Appreciate all the help.
– slnd54
Jul 1 at 2:54






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

U4lGctdTHrk 3nxaddmqtoC9 zQQMiIBeXnrVWQKnIC Xwa6x11w,RUOpR IqEQOdb2 dbIRpO2,nl,Cx5pRn5vuX ICcVRIN5Si0zrCT
9OgdiuTo58f4mG1G,PiA8WOXk 0,u77WuZIXcnMQ1j8kz 4,4jDg5TX,RSrpr3aZY,Czk zBpa60GtnDii767CnZI

Popular posts from this blog

PySpark - SparkContext: Error initializing SparkContext File does not exist

django NoReverseMatch Exception

Audio Livestreaming with Python & Flask