Note that there are some explanatory texts on larger screens.

plurals
  1. POphp executes before submit button is hit
    primarykey
    data
    text
    <p>I am trying to connect to my db via PDO like any other of my pages so I just copied and pasted the same code from my other page nothing changed (same db and user and such) and now I am getting this error : </p> <blockquote> <p>[Wed Dec 26 22:51:49 2012] [error] [client 127.0.0.1] PHP Notice: Undefined index: anum in /var/www/signinpage.php on line 69 [Wed Dec</p> <p>26 22:51:49 2012] [error] [client 127.0.0.1] PHP Notice: Undefined index: first in /var/www/signinpage.php on line 70 [Wed Dec 26</p> <p>22:51:49 2012] [error] [client 127.0.0.1] PHP Notice: Undefined index: last in /var/www/signinpage.php on line 71 [Wed Dec 26 22:51:49 2012] </p> <p>[error] [client 127.0.0.1] PHP Notice: Undefined index: why in /var/www/signinpage.php on line 72 [Wed Dec 26 22:51:49 2012] [error]</p> <p>[client 127.0.0.1] PHP Notice: Undefined index: comments in /var/www/signinpage.php on line 73 [Wed Dec 26 22:51:49 2012] [error]</p> <p>[client 127.0.0.1] SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'anum' cannot be null</p> </blockquote> <p>I think the script is starting before the submit button his which I don't want to happen as the script is an insert and form validation script.</p> <p>these are the connect lines ( where error is being caused ) </p> <pre><code>$host="localhost"; // Host name $username="root"; // Mysql username $password="testdbpass"; // Mysql password $db_name="test"; // Database name // Connect to server via PHP Data Object $dbh = new PDO("mysql:host=localhost;dbname=test;", $username, $password); $dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); </code></pre> <p>and this is the full code : </p> <pre><code>&lt;?php //Starting session session_start(); $host="localhost"; // Host name $username="root"; // Mysql username $password="testdbpass"; // Mysql password $db_name="test"; // Database name // Connect to server via PHP Data Object $dbh = new PDO("mysql:host=localhost;dbname=test;", $username, $password); $dbh-&gt;setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // Validation starts here if(empty($_POST) === false) { $errors = array(); $anum = $_POST['anum']; $first = $_POST['first']; $last = $_POST['last']; $why = $_POST['why']; $comments = $_POST['comments']; if (empty($anum) === true || empty($first) === true || empty($last) === true){ $errors[] = 'Form is incomplete please revise it!'; } else { if(ctype_alnum($anum) === false) { $errors[] = 'A number can only consist of alphanumeric characters!'; } if(strlen($anum) &gt; 9) { $errors[] = 'A number is incorrect!'; } if(strlen($anum) &lt; 9) { $errors[] = 'A number is incorrect!'; } if(ctype_alpha($first) === false) { $errors[] = 'First mame must only contain alphabetical characters!'; } if(ctype_alpha($last) === false) { $errors[] = 'Last name must only contain alphabetical characters!'; } if(empty($why)) { $errors[] = 'Please make sure to select the proper reasoning for your vistit today!'; } elseif ($why ==='Other' &amp;&amp; empty($comments)) { $errors[] = 'Please explain the nature of your visit in the comments box!'; } if (strlen($comments) &lt; 15) { $errors[] = 'Your explaination is short, please revise!'; } if(strlen($comments) &gt; 45) { $errors[] = 'Your explaintion is to long, please revise!'; } } if (empty($errors) === true) { header('location: signedin.php'); exit(); } } // Validations ends here // We start our insert statement here! try { $query = $dbh-&gt;prepare("INSERT INTO `students` (anum, first, last, why, comments) VALUES (:anum, :first, :last, :why, :comments)"); $query-&gt;execute( array( 'anum' =&gt; $_POST['anum'], 'first' =&gt; $_POST['first'], 'last' =&gt; $_POST['last'], 'why' =&gt; $_POST['why'], 'comments' =&gt; $_POST['comments'] )); } catch (PDOException $e) { error_log($e-&gt;getMessage()); die($e-&gt;getMessage()); } $dbh = null; ?&gt; &lt;html&gt; &lt;body&gt; &lt;title&gt;Student Signin Form&lt;/title&gt; &lt;table width="300" align="center" cellpadding="0" cellspacing="1" bgcolor="#CCCCCC"&gt; &lt;tr&gt; &lt;?php if(empty($errors) === false) { echo '&lt;h3&gt;'; foreach ($errors as $error) { echo '&lt;center&gt;&lt;li&gt;' , $error, '&lt;/li&gt;&lt;/center&gt;'; } echo '&lt;h3&gt;'; } ?&gt; &lt;form action="" method="post"&gt; &lt;td&gt; &lt;table width="100%" border="0" cellpadding="3" cellspacing="1" bgcolor="#FFFFFF"&gt; &lt;tr&gt; &lt;tr colspan="3"&gt;&lt;center&gt;&lt;/center&gt;&lt;strong&gt;Student Signin Form&lt;/strong&gt;&lt;/tr&gt; &lt;p&gt;Student ID Number: &lt;input type="text" name="anum" &lt;?php if (isset($_POST['anum']) === true) {echo 'value="' ,$_POST['anum'], '"';} ?&gt; /&gt; &lt;p&gt;First Name: &lt;input type="text" name="first" &lt;?php if (isset($_POST['first']) === true) {echo 'value="' ,$_POST['first'], '"';} ?&gt; /&gt; &lt;p&gt;Last Name: &lt;input type="text" name="last" &lt;?php if (isset($_POST['last']) === true) {echo 'value="' ,$_POST['last'], '"';} ?&gt; /&gt; &lt;p&gt;How may we help you? &lt;select name="why" /&gt; &lt;option value=""&gt;&lt;/option&gt; &lt;option value="Appeal"&gt;Appeal&lt;/option&gt; &lt;option value="Other"&gt;Other: Please specify the nature of your visit bellow&lt;/option&gt; &lt;/select&gt; &lt;/tr&gt; &lt;br&gt; &lt;P&gt;If other please describe the issue you are having.&lt;/P&gt; &lt;textarea rows="10" cols="50" name="comments" &lt;?php if (isset($_POST['comments']) === true) {echo 'value="' ,$_POST['comments'], '"';} ?&gt;&gt;&lt;/textarea&gt; &lt;input type="submit" name="submit" value="Send"/&gt; &lt;/form&gt; &lt;/table&gt; &lt;/body&gt; &lt;/html&gt; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload