0

Hello I'd like to insert all from $_SESSION into rows $key with value $value so something like foreach ($_SESSION as $key => $value) {} Imlooking at:Best way to INSERT many values in mysqli? name of rows in mysqli is same as names of given $key .I need to insert each $value in its $key (row)

Code:

$query = "INSERT INTO testtable VALUES (?)";
$stmt = $dbc->prepare($query);
$stmt->bind_param("s", $key);
$mysqli->query("START TRANSACTION");
foreach ($_SESSION as $key => $value) {
    $stmt->execute();
}
$stmt->close();
$mysqli->query("COMMIT");
Community
  • 1
  • 1
Newb
  • 27
  • 1
  • 6

2 Answers2

0

Your query has a syntax error, which you never bothered checking for:

$query = "INSERT INTO testtable podatki VALUES (?)";
                      ^^^^^^^^^^^^^^^^^

If that's really the table name, then it should be quoted with backticks:

$query = "INSERT INTO `testtable podatki` VALUES (?)";
                      ^-----------------^

if podatki is a field name, then it should be

$query = "INSERT INTO testtable (podatki) VALUES (?)";
                                ^-------^

And also never assume that a DB operation succeeded. ALWAYS check for errors:

$stmt = $dbc->prepare($query);
if (!$stmt) {
   die(mysqli_error($dbc));
}
Marc B
  • 356,200
  • 43
  • 426
  • 500
0

The error is saying that you are trying to call a member function, namely bind_param(), on a non-object.

That means that this line:

$stmt = $dbc->prepare($query);

is not succeeding,

and thus you have an incorrect return value that is set as the value of $stmt

so when you try to call bind_param it fails because $stmt is not the type of object that it was expecting.

Alex W
  • 37,233
  • 13
  • 109
  • 109
  • Any idea waht to do? I don't understand the code. object programming in php is confusing to me – Newb Sep 25 '14 at 17:14
  • @Newb Add a line after `$stmt = $dbc->prepare($query);` with this code: `var_dump($stmt); die;` and edit your question to post the results. – Alex W Sep 25 '14 at 17:17
  • bool(false) is what i get. $dbc is the $dbc = new mysqli(stuff..); right? Can you also check logics behind this code.? – Newb Sep 25 '14 at 17:19
  • @Newb http://stackoverflow.com/questions/1219809/mysqli-prepare-statement-returning-false-but-why – Alex W Sep 25 '14 at 17:22
  • ok i fixed query, copied something wrong. now i get Column count doesn't match value count at row 1. row one is primary "id" with auto incremnt? what does it mean. there must be a logical problem which i dont understand – Newb Sep 25 '14 at 17:25
  • @Newb It means that you are inserting more or less values than there are columns in your database table. Look at the [MySQL syntax for insert](http://dev.mysql.com/doc/refman/5.6/en/insert.html). – Alex W Sep 25 '14 at 17:27
  • Thats true. is there a way to not fill all the columns? – Newb Sep 25 '14 at 17:28
  • Only row thats not used is first one. "id" which is primary and auto increment – Newb Sep 25 '14 at 17:36
  • Only row thats not used is first one. "id" which is primary and auto increment. there must be logical error. Can you check if this insert all from $_SESSION into rows $key with value $value? – Newb Sep 25 '14 at 17:41
  • @Newb When you do the `INSERT` you don't have to specify an `id` when you just use `INSERT VALUES(...)` it is implicit and just auto increments itself. **However** if you want to insert the `id` you have to use the other syntax: `INSERT INTO tbl_name (id,...) VALUES(15,...);` – Alex W Sep 25 '14 at 17:43
  • no there is no id insert there must be logical error – Newb Sep 25 '14 at 17:45
  • @Newb Can you edit your question and show what the columns are in your database table and what values you are inserting in an example row? – Alex W Sep 25 '14 at 17:48
  • @Newb I believe the problem is you are trying to bind multiple values to a single `?`. http://stackoverflow.com/questions/13004870/inserting-multiple-values-using-mysqli – Alex W Sep 25 '14 at 18:10
  • Hey, i have updated my question. All of the columns are named after $key from $_SESSION i just need to add their values. – Newb Sep 25 '14 at 18:33