0

Hello I have this code that won't work. I've been bugging with it for about an hour and can't seem to find any errors, I don't know what it is. Maybe someone could help me here.

Inserting script :

    <?php error_reporting(E_ALL); ini_set('display_errors',1);
require('connect.php');

$sql = "INSERT INTO products (name, description, price, url, category, artwork) VALUES ('john', 'john', 'john', 'john', 'john', 'john')";
if ($link->query($sql)) {
  echo "<script>
  alert('Data was added.');
  window.location.href='dashboard.php';
  </script>";
}
else {
  echo "<script>
  alert('Data was not added.');
  window.location.href='dashboard.php';
  </script>";
}
 ?>

I am trying to add data to my database, but it just won't add the data that i've given it.

Here is my connect script :

    <?php

try {
  $dbhost = 'localhost';
  $dbuser = 'root';
  $dbpass = '';
  $link = new PDO("mysql:host=$dbhost;dbname=dbtesttest;",$dbuser,$dbpass);

} catch (PDOException $e) {
  echo "Failed :" . $e->getMessage() . "\n";
}

?>

Anyone got a clue what I am doing wrong? Besides using alerts in php. It doesn't give me any errors, I have a database made with the actual attributes. I need help :(

  • 1
    Have you checked that the query is working? The first thing that comes to mind is putting a string in a numeric field (price -> 'john') – 3472948 Feb 21 '17 at 23:29
  • Try to use prepared statements and execute your query after preparation. More here http://php.net/manual/en/pdo.prepared-statements.php – Grynets Feb 21 '17 at 23:36
  • What does "won't work" mean? You have an if/then/else statement -- which of the code branches is executed? – miken32 Feb 22 '17 at 00:54

2 Answers2

-1

I think you should use $link->exec($sql);

  • Why do you think that? Provide something more than a passing comment with nothing to back it up. – miken32 Feb 22 '17 at 00:33
-1

Let's try to simplify the issue. It does not look like you are returning the database connection. So that you can understand better, we can call the db from the same script (we can move it out after you get it working because we all love to refactor don't we?)

try {
  $dbhost = 'localhost';
  $dbuser = 'root';
  $dbpass = '';
  $link = new PDO("mysql:host=$dbhost;dbname=dbtesttest;",$dbuser,$dbpass);

//set attribute to connection in the try block

 $link->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);


} catch (PDOException $e) {
  echo "Failed :" . $e->getMessage() . "\n";
}

then change this line

$link->query($sql)

to

$stmt = $link->prepare($sql);
$stmt->execute();

Everything above should go in your try block for now and the insert should work or it will return a more detailed error. Hopefully that will get you going and help you wrap your head around it. This link has a good primer on php/mysql CRUD operations.

eddyizm
  • 535
  • 5
  • 12
  • What's wrong with calling `$link->query` directly? Why do you think that preparing a statement would help? – miken32 Feb 22 '17 at 00:34
  • I don't think it is wrong but for the OP, it should help him discern what is going on and hopefully better understand the operations. I believe it also reads better when writing insert/update/delete statements as opposed to selects as query is often associated with. This [link](http://stackoverflow.com/questions/16381365/difference-between-pdo-query-and-pdo-exec) explains the difference in detail. – eddyizm Feb 22 '17 at 00:49
  • 1
    Answers should be answering the question, not troubleshooting or making things "read better." Once you've got a bit more rep you'll be able to comment on questions to seek clarifications or provide debug suggestions. – miken32 Feb 22 '17 at 00:53