0

I am trying to insert data into my database. When I execute the statement I get no errors in PHP. When I execute the sql statement in my mysql database I get the error: Unknown column 'test 'in field list.

Here is the script that I use to insert data:

if($correct) {
    $db = new PDO('mysql:host=localhost;dbname=db', 'root', '');

    $query = "INSERT INTO scu(user_id, internal_id, name) VALUES (?, ?, ?)";
    $stmt = $db->prepare($query);
    $stmt->execute(array($_SESSION['user_id'], $internal_id, $name));

    header("Location: ../succ.php"); 

I get the error when I execute the following statement:

INSERT INTO scu(user_id, internal_id, name) VALUES (1, 1, test)

Does someone know what is wrong with my script?

Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
John
  • 904
  • 8
  • 22
  • 56
  • 2
    Did you mean `'test'`, ie a string – Phil Jan 10 '19 at 01:33
  • Wrapping test into quotes will work for the MySQL part of the code. But not for the PHP – John Jan 10 '19 at 01:34
  • That's right, because your PHP is (correctly) using a prepared statement with parameter binding. See http://php.net/manual/pdo.prepared-statements.php – Phil Jan 10 '19 at 01:35
  • But I am using this same script for other tables. How can it be possible that I have no issues with other tables? – John Jan 10 '19 at 01:43
  • 1
    I don't understand what you're asking. If you want to insert the string "test" into your table by running a query directly (not via a PHP prepared statement), you need to quote the string, ie `'test'` instead of `test`. Otherwise, MySQL thinks you want to reference a column named "test", hence your error message – Phil Jan 10 '19 at 01:45
  • Ok. The MySQL part is clear. But why is the PHP part of the code not working? I am using this script also for adding data to other tables. The other tables contain also `user_id`, `internal_id` and `name` and all these fiels have the same structure. – John Jan 10 '19 at 22:12
  • The most interesting part is that this script worked before. I created two rows with the same script. – John Jan 10 '19 at 22:12
  • If you now have a problem with your PHP code, I suggest you open a new question. Please also make sure you [set PDO to throw exceptions](https://stackoverflow.com/a/52324601/283366) and include any error messages and all relevant code in your question. – Phil Jan 10 '19 at 23:08
  • When PHP sees a collection of letters that are not inside of quotes, it will default to assuming it's a string. E.g., both `$x = 'test';` and `$x = test;` both result in setting $x to the string 'test'. It should issue a warning message for that, but you might not see that warning depending on how it's configured. The correct way in all cases is to enclose the string in quotes. PHP can be forgiving of doing it less than the correct way, which can be confusing. – UncaAlby Jan 10 '19 at 23:14
  • I am defining the variables with a post. Example: `$name = $_POST['name'] ;`. It always worked for me. How can I put the post in a quote? – John Jan 10 '19 at 23:21
  • *"I get the error when I execute the following statement: INSERT INTO scu(user_id, internal_id, name) VALUES (1, 1, test)"* - When you execute *that* or the query with the placeholders? And what is the db schema/column types? – Funk Forty Niner Jan 10 '19 at 23:22
  • Is `user_id` an AI'd (AUTO_INCREMENT) column? I'm thinking there could be a column shift happening. – Funk Forty Niner Jan 10 '19 at 23:24
  • No, user_id is not an AI column. `user_id=int(11)`, `internal_id=int(11)` and `name=varchar(255)` – John Jan 10 '19 at 23:26
  • @John please open a new question as this one is not asking about your PHP code (and it's already closed) – Phil Jan 10 '19 at 23:26
  • Are you 100% sure you used the right database and table? I've seen that happen before. – Funk Forty Niner Jan 10 '19 at 23:31
  • @FunkFortyNiner I know. I've also seen thinks like that before. But that's not the case here. The database, tables, columns and variables are all correct – John Jan 10 '19 at 23:34
  • Can you try http://php.net/manual/en/pdo.error-handling.php adding that and see? and make sure the session array has value. – Funk Forty Niner Jan 10 '19 at 23:35
  • @FunkFortyNiner I tried to echo echo the session and I get the correct result `1`. The error handling is not returning any errors – John Jan 10 '19 at 23:48

0 Answers0