-1

I have a little problem with this code I have. When I run it that doesn't create any values in database and shows.

Parse error: syntax error, unexpected 'INSERT' (T_STRING) in /home/vol6_8/epizy.com/epiz_23744660/htdocs/main/test.php on line 8

This is the code I have:

include("config.php);

// Create connection
$conn = new mysqli($dbhost, $dbuser, $dbpasswd, $dbname);

// prepare and bind
$stmt = $conn->prepare("INSERT INTO phpbb_crany ('crany_id', 'crany_name', 'crany_breed', 'crany_gender', 'crany_level', 'crany_born', 'crany_mother', 'crany_father', 'crany_element', 'crany_user') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

$stmt->bind_param("ssssssssss",1, $cn, $cb, $cg, $cl, $cb, $cm, $cf, $ce, $cu);

// set parameters and execute
$cn = $_POST['petname'];

$cb = "jgh";

$cg = rand(0,1);

$cl = "1";

$cb = $_SERVER['REQUEST_TIME'];

$cm = 0;

$cf = 0;

$ce = 1;

$cu = 1;

$stmt->execute();

echo "New records created successfully";

$stmt->close();
$conn->close();

i'm trying with php and mysql. what'ss the problem?

Ivar
  • 6,138
  • 12
  • 49
  • 61
EchoDino
  • 35
  • 1
  • 10

1 Answers1

0

You have an logic misstake in your code. Your code (the bind_param()-function` is excecuted, before you set your varibales.

Try it so:

include("config.php");

$stmt = $conn->prepare("INSERT INTO phpbb_crany ('crany_id', 'crany_name', 'crany_breed', 'crany_gender', 'crany_level', 'crany_born', 'crany_mother', 'crany_father', 'crany_element', 'crany_user') VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?)");

$cb = "jgh";
$cg = rand(0,1);
$cl = "1";
$cb = $_SERVER['REQUEST_TIME'];
$cm = 0;
$cf = 0;
$ce = 1;
$cu = 1;
$stmt->bind_param("ssssssssss",1, $cn, $cb, $cg, $cl, $cb, $cm, $cf, $ce, $cu);// <---- CHANGED - line moved

$stmt->execute();
echo "New records created successfully";
$stmt->close(); $conn->close();
Richard
  • 618
  • 1
  • 9
  • 15
  • That's not true. `bind_param()` is by reference, so you can actually define them *after* the function call. But they must be set before `execute()`. – Qirel May 25 '19 at 14:46
  • But you must define the `$cb` variable before you insert it in your `bind_param()` function. – Richard May 25 '19 at 15:56
  • No, you must not - which is exactly my point. The issue in the question is the missing quote in `include("config.php");`. If you don't believe me, see [the example from the PHP manual](https://www.php.net/manual/en/mysqli-stmt.bind-param.php#refsect1-mysqli-stmt.bind-param-examples). Your answer only solves the issue in the question because you fixed the missing quote - it has nothing to do with the order of definition of variables (so long as its before the `execute()`). – Qirel May 25 '19 at 16:38
  • nether worked :) i used PDO and solved – EchoDino May 29 '19 at 14:56