1

I have a project that I am using OpenJS grid with a Bootstrap modal to POST rows to the table. I have used this basic outline before and it has always worked. But now with using these new elemnts I cant POST to the database. The form looks like:

  <!-- Add Message Form--------------------------------------------------------------------------->
        <div class="container">
  <h2>Memo</h2>
  <form role="form" action="add.php" method="post">
    <div class="form-group">
      <label for="from">From:</label>
      <input type="text" class="form-control" id="from" name="from_val" placeholder="Message From">
    </div>
    <div class="form-group">
      <label for="note">Enter Message:</label>
      <input type="textarea" class="form-control" rows="5" id="note" name="note_val" placeholder="Enter Your Message">
    </div>
    <div class="form-group">
      <label for="date_entered">Enter Date:</label>
      <input type="date" class="form-control" id="date_entered" name="date_val" placeholder="yyyy/mm/dd">
    </div>
    <div class="form-group">
      <label for="time_entered">Enter Time:</label>
      <input type="text" class="form-control" id="time_entered" name="time_val" placeholder="hh:mm:ss">
    </div>
    <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
    <button type="submit" id="newData" class="btn  btn-primary btn-md">Submit</button>
  </form>
   </div>
     <!-- End Form ------------------------------------------------------------------------------------> 
    </div>

The php script is:

<?php

ini_set('display_errors',1); 
error_reporting(E_ALL);


header("Access-Control-Allow-Origin: *");
header("Content-Type: application/json; charset=UTF-8");
include ("dbinfo.inc.php"); //include login info file

//Start a PDO session to insert the form data into the MySQL Table

try
    {
    $conn=new PDO($dsn, $username,$password);
    echo 'Connected to MySQL Ok.';
    }
catch (PDOException $e)
    {
    $error_message=$e->getMessage();
    echo "<h1>Resource Unavailable. Please Contact the System Administrator</h1>";
    }

$from=$_POST['from_val'];
$note=$_POST['note_val'];
$date=$_POST['date_val'];
$time=$_POST['time_val'];
print_r ($_POST);


if ($from !='') { $stmt = $conn->prepare("INSERT INTO Messages SET from = :from, note = :note, date = :date, time = :time");


     $stmt->execute(array(
            ':from' => $from,
            ':note' => $note,
            ':date' => $date,
            ':time' => $time));


echo '<br /n>';
echo '<br /n>';
echo '<h3><center><b>Added Record Successfully</b></center></h3>';
  }

else {
echo '<br /n>';
echo '<br /n>';
echo 'Attention. You Did Not Enter a Message. Please enter the Message<br /n>';
echo 'At a Minimum..';
}
?>

I checked the php and it validates ok. On the add.php page I get this after adding a record:

Connected to MySQL Ok.Array
(
    [from_val] => alan
    [note_val] => test message.
    [date_val] => 2015/03/22
    [time_val] => 12:00:10
)
<br /n><br /n><h3><center><b>Added Record Successfully</b></center></h3>

This does not look right to me. I dont remember POST info in this format before when I was troubleshooting issues.

Alan
  • 1,067
  • 1
  • 23
  • 37
  • The problem isn't with your POST array(s), it's with the MySQL reserved word you're using in your query. http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html - Add `$con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` right after the connection is opened and you will see the error. – Funk Forty Niner Mar 10 '15 at 14:59
  • I'm sorry what is the reserved word I'm using? I dont see it listed in any table. – Alan Mar 10 '15 at 15:03
  • 1
    Reload my comment and add that to your connection. You will see the error, then you will see which one is the reserved word. Typo, should have been `$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);` and the link to the list http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html – Funk Forty Niner Mar 10 '15 at 15:04
  • So, how you making out? – Funk Forty Niner Mar 10 '15 at 15:20
  • sorry. changed both date & time to curr_date & curr_time in php and MySql database but get this:Fatal error: Uncaught exception 'PDOException' with message 'SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from = 'alan', note = 'Another test Message. Okkkk', curr_date = '2015/03/22', c' at line 1' in /var/www/bb/add.php:39 – Alan Mar 10 '15 at 15:24
  • Examine the error. `You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from` and where it is starting from. Then visit http://dev.mysql.com/doc/refman/5.5/en/reserved-words.html and look at the list. – Funk Forty Niner Mar 10 '15 at 15:24
  • Once you've found it, visit the link I used to close the question with http://stackoverflow.com/questions/23446377/syntax-error-due-to-using-a-reserved-word-as-a-table-or-column-name-in-mysql and you will see how to deal with it. – Funk Forty Niner Mar 10 '15 at 15:31
  • Ok. I'm just not seeing it. I think it is the time column. I changed it to crnt_time in both php & database and stillget the error on line 39 which is ':crnt_time' => $crnt_time)); – Alan Mar 10 '15 at 15:32
  • Ok, no sense in going on with this all day. Here. The word in question, is "from". I've given you enough comments/information for you to fix it. – Funk Forty Niner Mar 10 '15 at 15:33
  • Sorry the line 39 was throwing me off. I kept on looking at time. Changed from and all is well. Thanks for your patience. You are right to teach a man to fish and feed him for life. Thanks again, – Alan Mar 10 '15 at 15:44
  • That's what Stack's all about, to help and teach all at the same time. I'm glad this was resolved and was happy to have been of help, *cheers!* - Oh, and I grow "cactus", really. So, I have a lot of patience. – Funk Forty Niner Mar 10 '15 at 15:45
  • P.s.: Don't always believe what line PHP's telling you where the error is; it doesn't always apply. – Funk Forty Niner Mar 10 '15 at 15:46

0 Answers0