-1

I'm getting an error

Here's the code:

<?php

//db connection

$dbserv="localhost:3306";
$dbuser="root";
$dbpass="firepower";
$dbname="account_data";
$db_connect=new mysqli($dbserv, $dbuser, $dbpass, $dbname);

if($db_connect->connect_errno){
    die("Error trying to connect to database");
}

session_start();

if(isset($_SESSION['current_page'])){
  $prev_page=$_SESSION['current_page'];
  $_SESSION['current_page'] = basename($_SERVER['PHP_SELF']);
}
else{
  $prev_page='';
}

if(!isset($_SESSION['email'])){
  header('location:'.'index.php?_rdr');
  die();
}

$limit=$_SESSION['limit'];
$index=1;
$first=true;
$products_ordered='';

while($index<=$limit){
  if(isset($_POST[$index]) && $first==true){
    $products_ordered.=$_POST[$index].' x Pizza '.$_SESSION['prod_name'][$index];
    $first=false;
  }
  else if(isset($_POST[$index]) && $first==false){
    $products_ordered.=', '.$_POST[$index].' x Pizza '.$_SESSION['prod_name'][$index];
  }
  $index++;
}

//insert order into db

$user_email=$_SESSION['email'];
$total_price=$_SESSION['order_price'];

$query="INSERT INTO orders (email, products_ordered, total_price) VALUES (?, ?, ?)";
$sql_sec=$db_connect->prepare($query);
$sql_sec->bind_param("ssi", $user_email, $products_ordered, $total_price);
$sql_sec->execute();
$result=$sql_sec->get_result();


if(mysqli_num_rows($result)){
  exit("Order added successfully!");
}
else{
  exit("Error connecting to database!");
}

?>

It's so strange because data is added correctly to my database and the other times I used the same code (for user registration) it worked with no problems... I checked everything and I can't find the problem.

$query="INSERT INTO orders (email, products_ordered, total_price) VALUES (?, ?, ?)";
$sql_sec=$db_connect->prepare($query);
$sql_sec->bind_param("ssi", $user_email, $products_ordered, $total_price);
$sql_sec->execute();
$result=$sql_sec->get_result();


if(mysqli_num_rows($result)){
  exit("Order added successfully!");
}
else{
  exit("Error connecting to database!");
}

The error I'm getting is this:

Warning: mysqli_num_rows() expects parameter 1 to be mysqli_result, boolean given in C:\xampp\htdocs\Dream Pizza\add_order.php on line 58

Error connecting to database!

River
  • 8,585
  • 14
  • 54
  • 67
  • Use `mysqli_error()` to get a better error message. -> http://php.net/manual/en/mysqli.error.php – Raymond Nijland Apr 22 '18 at 13:11
  • You need to remove the port from `$dbserv="localhost:3306";` From the PHP docs "`mysqli::__construct ([ string $host` parameter "`host ..." Can be either a host name or an IP address.`" Doesn't look like the manual say it's posssible to add the port to the host string. -> http://php.net/manual/en/mysqli.construct.php besides there is a seperated port parameter within the mysqi contructor. – Raymond Nijland Apr 22 '18 at 13:14

1 Answers1

1

From the documentation for the get_result() function (emphasis mine):

Returns a resultset for successful SELECT queries, or FALSE for other DML queries or on failure.

You're executing an INSERT query, so get_result() will return false.

Instead, you should be using the mysqli_stmt_* functions (or just access the properties directly) to obtain information about the query's result from your $sql_sec statement directly:

if(mysqli_stmt_affected_rows($sql_sec)){
// or:
if($sql_sec->affected_rows) {

Note that you need to check the affected_rows property, not the num_rows property. The reason is that num_rows returns the number of rows in the result set. You're not selecting a result set, you're inserting a new set of data. That means you're actually interested in the number of rows that was inserted, which is stored in the affected_rows property.

rickdenhaan
  • 10,857
  • 28
  • 37