Bitan Basak
Bitan Basak

Reputation: 61

Getting wrong result in PHP/MySQLi

I am trying to insert the name and age of a user from a form into a database named users. On successful insertion I want to display a success message. Here is my code but it's somehow printing the failure message even on successful insertion:

<?php require_once('Connections/Localhost.php'); ?>
<?php

$success = NULL;

if(isset($_POST['AddUser'])) {

    session_start();

    $name = $_POST['Name'];
    $age = $_POST['Age'];

    $stmt = $con->prepare("INSERT INTO employees(Name, Age)Values(?, ?)");

    $stmt->bind_param('ss', $name, $age);

    $stmt->execute();

    $result = $stmt->get_result();

    if($result) {

    $success = "Added Successfully!";   

    }
    else {

    $success = "Sorry! Couln't add!";   

    }

}

?>
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Add User</title>
<link href="Stylesheet/form.css" rel="stylesheet" type="text/css">
</head>

<body>
<div id="form">
<form action="index.php" id="form1" name="form1" method="POST">
  <div class="formfield"><input name="Name" type="text" id="Name" placeholder="Name"></input><br></div>
  <div class="formfield"><input name="Age" type="number" id="Age" placeholder="Age"></input><br></div>
  <div class="formbutton"><input name="AddUser" type="submit" id="AddUser" value="Add"></input></div>
</form>

<?php 

echo "<br>";
echo $success; 

?>
</div>
</body>
</html>

Can anyone please point out the mistake?

Upvotes: 1

Views: 78

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74232

Seeing an answer (and comment if it doesn't get deleted) based on my comment, I'll submit it as an answer:

get_result() "Returns a resultset for successful SELECT queries, or FALSE for other DML queries or on failure." http://php.net/manual/en/mysqli-stmt.get-result.php

Use affected_rows() http://php.net/manual/en/mysqli.affected-rows.php

"Returns the number of rows affected by the last INSERT, UPDATE, REPLACE or DELETE query."

You should also check for errors against your query by changing:

$stmt->execute();

to:

if(!$stmt->execute()){
   trigger_error("There was an error....".$con->error, E_USER_WARNING);
}

Should there be any errors while attempting to do the insert.

Reference:

Add error reporting to the top of your file(s) which will help find errors.

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

// Then the rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Upvotes: 1

devpro
devpro

Reputation: 16117

You can not use get_result() here you can use lastInsertid(), it will give you the last inserted id and than you can use condition as:

<?
$stmt = $con->prepare(" your query ... ");
$stmt->bind_param('ss', $name, $age);

$stmt->execute();
$lastId = $con->lastInsertId(); // add this 
if($lastId > 0) {
    $success = "Added Successfully!";   
}
else {
    $success = "Sorry! Couln't add!";   
}
?>

Upvotes: 0

Related Questions