0

I am trying to make a page that allows the user to enter the steps he will take to do a particular task. However, whenever I submit a step, multiple similar records get stored inside mysql database. So if i type "Do this", in my mysql database, two records will be stored with same data Here is my code...

<?php

$conn = new mysqli("localhost", "root","","KFC");
if(isset($_POST['Submit_btn']))
{
  session_start();
    $var_value = $_SESSION['R_ID'];

    $Name= mysqli_real_escape_string($conn, $_POST['Step']);

    $sql= "INSERT INTO `step`(`Step`, `R_ID`) VALUES ('$Name','$var_value')";

    mysqli_query($conn,$sql);
    if (!mysqli_query($conn,$sql))
  {
  echo("Error description: " . mysqli_error($conn));
  }
}
mysqli_close($conn);
?>
<html>
<body>
<div id="bodyContent">
<h1> Submit Steps </h1>
</div>
<?php
mysql_connect('localhost', 'root', '');
mysql_select_db('garden');
$result = mysql_query("SELECT * FROM step WHERE R_ID='$var_value'");
$rows = mysql_num_rows($result);
echo "So far you have submitted " . $rows . " steps for your recipe.";
?>
<form method="post">
<table>
<tr>
<td>STEP: </td>
<td>
<textarea type="text" name="Step" class="textInput"></textarea>
</td>
</tr>
<tr>
<td> </td>
<td>
<input type="submit" name="Submit_btn" value="submit">
</td>
<td>
<input type="submit" name="Continue_btn" value="Continue">
</td>
</tr>

</table>
</form>
</body>
</html>
5
  • 2
    You are executing the query twice. Remove one Commented Apr 9, 2018 at 2:38
  • how am i executing it twice :/ ? the first one is for inserting and second is for viewing Commented Apr 9, 2018 at 2:44
  • @AkintundeOlawale ^ Commented Apr 9, 2018 at 2:44
  • mysqli_query($conn,$sql); if (!mysqli_query($conn,$sql)) = twice Commented Apr 9, 2018 at 2:45
  • your also using mysql and myqli in the same script for no reason. making 2 db connections etc. Commented Apr 9, 2018 at 2:46

1 Answer 1

1

You are running the execution query twice .

mysqli_query($conn,$sql);

This instruction execute the insert query that you have. You are executing it once, and then you are executing again to validate the boolean return value of the function. You need to use only the validation row.

This is what you have:

  mysqli_query($conn,$sql);
  if (!mysqli_query($conn,$sql))
  {
    echo("Error description: " . mysqli_error($conn));
  }

You only need:

  if (!mysqli_query($conn,$sql))
  {
    echo("Error description: " . mysqli_error($conn));
  }

The validation row will execute the function and return the boolean value you need.

Sign up to request clarification or add additional context in comments.

1 Comment

glad to help ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.