1

I cant figure out why my php script isn't working at times it does work after deleting all data from my database table

<?php

$con = mysqli_connect("localhost","root","","db_csbn");

if(mysqli_connect_errno())
{
    echo "MySQLi Connection was not established: " . mysqli_connect_error();
}

?>

...

<?php

include "connect.php";

$tor = (isset($_POST['tor']) ? "1" : "0");
$frm = (isset($_POST['frm']) ? "1" : "0");
$dp = (isset($_POST['dp']) ? "1" : "0");
$gm = (isset($_POST['gm']) ? "1" : "0");
$hd = (isset($_POST['hd']) ? "1" : "0");
$cr = (isset($_POST['cr'])? "1" : "0");
$ch = (isset($_POST['ch']) ? "1" : "0");
$snumber = $_POST['snumber'];


$documents = $tor.$frm.$dp.$gm.$hd.$cr.$ch;
echo $documents;
$date = date("Y-m-d");
if($tor || $frm || $dp || $gm || $hd || $cr || $ch)
{
    $id = str_replace("-","",$date).$snumber;


    $sql = "INSERT INTO `tbl_requests` (`id`, `dr`, `snumber`, `date`) VALUES('".$id."', '".$documents."', '".$snumber."', '".$date."');";

    if( mysqli_query($con, $sql))
    {
        echo"<script>alert(Request id (Please Save):\n $id);</script>";
    }
}
}
?>

i cant see any errors after running this script but no data was added to my table.

RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Thamus
  • 108
  • 8
  • 1
    After the `}` of the `query` do `else` and check for errors. http://php.net/manual/en/mysqli.error.php – chris85 Sep 01 '16 at 18:34
  • i added if( mysqli_query($con, $sql)) { echo""; } else { echo ""; } even the alert box isn't showing up – Thamus Sep 01 '16 at 18:38
  • Well `asd` is kind of useless, but what did that give you? – chris85 Sep 01 '16 at 18:39
  • nothing, it gave me nothing :( – Thamus Sep 01 '16 at 18:39
  • So you have a parsing error and are getting a 500? – chris85 Sep 01 '16 at 18:39
  • im sorry im not familiar with parsing error. how can i know if i got one? – Thamus Sep 01 '16 at 18:40
  • Check your error logs. Checking the status code of your page should let you know if you have an error. You have three `}`s but only two `{`. – chris85 Sep 01 '16 at 18:41
  • Add `mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);` to the top of your script. This will force any `mysqli_` errors to generate an Exception that you cannot miss or ignore. – RiggsFolly Sep 01 '16 at 18:55
  • Also look at your php error log and/or add these 2 lines to the top of any script you are debugging `error_reporting(E_ALL); ini_set('display_errors', 1);` Just after the first ` – RiggsFolly Sep 01 '16 at 18:56
  • 1
    If it works after deleting all data from your table, then the most likely cause is that the subsequent data you attempt to insert violates a unique constraint. I would suspect `id` first. Are you using the same value for `snumber` each time? Enable error reporting as suggested and it won't be such a guessing game. – Don't Panic Sep 01 '16 at 18:56
  • actually im using the same snumber each time i try to run the script – Thamus Sep 01 '16 at 18:59
  • Is `id` the primary key? – Don't Panic Sep 01 '16 at 18:59
  • id is the primary key, i tried the error logs and it gave me `Duplicate entry '2016090102120130170' for key 'PRIMARY'` – Thamus Sep 01 '16 at 18:59
  • 2
    The problem is, the way you're building `id` will not make it unique, and it must be unique in order for a new row to be inserted. Any reason you need an id like that rather than a plain autoincrement integer? You can put a timestamp in another column if you need to know when the row was inserted. – Don't Panic Sep 01 '16 at 19:02
  • thanks dude it worked ;) – Thamus Sep 01 '16 at 19:05

1 Answers1

-1

First of all you've got too many }

After query do else with printf("Error: %s\n", mysqli_error($link));

Knowledge
  • 145
  • 1
  • 9