Jump to content

My insert code is not posting into the database


terungwa
Go to solution Solved by Barand,

Recommended Posts

My insert code is not posting into the database when i wrap the insert query between this conditional statement: if(empty($reg_errors)){ ...post to database..}, would appreciate your thoughts.

<?php
require_once('./includes/connection.inc.php');
  // create database connection
  $conn = dbConnect('read');
if (isset($_POST['insert'])) {
// Define post fields into simple variables
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$sex = $_POST['sex'];

// verify register_donor input // check last_name field
if(!preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["last_name"]))
    {
        // set error for last_name field
        $reg_errors['last_name'] = '<p class="warning">Last Name must contain letters, dashes and spaces only and must start with upper case letter</p>';
    }
if(!preg_match("/^[A-Z][a-zA-Z -]+$/", $_POST["first_name"]))
    {
        // set error for first_name field
        $reg_errors['first_name'] = '<p class="warning">First Name must contain letters, dashes and spaces only and must start with upper case letter</p>';
    }
if(!preg_match("/^[a-zA-Z]+$/", $_POST["sex"]))
    {
        // set error for sex field
        $reg_errors['sex'] = '<p class="warning">Donor sex must contain letters only and must start with upper case letter</p>';
    }
    // if no errors are set, inset record into database
if(empty($reg_errors)){  
  // initialize flag
  $OK = false;
  // initialize prepared statement
  $stmt = $conn->stmt_init();
  // create SQL
  $sql = 'INSERT INTO register_donor (
			donor_id,
			last_name,
			first_name,
			sex)
		VALUES(?, ?, ?, ?)';
  if ($stmt->prepare($sql)) {
	// bind parameters and execute statement
	$stmt->bind_param('isss', $_POST['donor_id'], $_POST['last_name'],$_POST['first_name'], $_POST['sex']);
    // execute and get number of affected rows
	$stmt->execute();
	if ($stmt->affected_rows > 0) {
	  $OK = true;
	}
  }
  // redirect if successful or display error
  if ($OK) {
	echo 'success';
	exit;
  } else {
	$error = $stmt->error;
  }
}}
?>
<!DOCTYPE html>
<html lang="en">
<head>
</head>
	<body>
	    <div class="container_12 main">
		<div class="grid_12">
		      <div class="grid_8 omega" id="main">
			    <fieldset>
				<legend><h2>Register New Donor:</h2></legend>
				     <p>
					<?php  
						if (isset($error)) {
						echo "<p class=\"warning\">Error: $error</li>";
						} 
					?>
					</p>
					<form id="form1" method="post" action="">			  
						<table border="0">
							<tr>
							     <th>
								<label for="last_name">Last Name:</label>
							     </th>
							     <td>
								<?php if(isset($reg_errors['last_name'])) echo $reg_errors['last_name'];?>
								<input type="text" name="last_name" class="widebox" required aria-required="true" id="last_name" <?php if(isset($reg_errors)) { 
								 echo 'value="'.htmlentities($last_name, ENT_COMPAT, 'UTF-8') .'"';
								} ?>>
							     </td>
							     </tr>
							     <tr>
								<th><label for="first_name">First Name:</label></th>
								 <td>
								    <?php if(isset($reg_errors['first_name'])) echo $reg_errors['first_name'];?>
								    <input name="first_name" type="text" class="widebox" required aria-required="true" id="first_name" <?php if(isset($reg_errors)) { 
								     echo 'value="'.htmlentities($first_name, ENT_COMPAT, 'UTF-8') .'"';
								     } ?>>
								   </td>
								   </tr>
								   <tr>
								 	<th><label for="sex">Sex:</label></th>
									<td>
								        <?php if(isset($reg_errors['sex'])) echo $reg_errors['sex'];?>
									 <select name="sex" id="sex" required aria-required="true">
									   <option>													
									   <option>
									   <option>
									     Male
									   </option>
									   <option>
									     Female
									   </option>
									  </select>
									</td>
									</tr>
									
								</table>
							<p>
							<input type="submit" name="insert" value="Register New Donor" id="insert">
						    </p>
						</form>
						</fieldset>
			        </div>	
				</div>
	</body>
</html>
Edited by terungwa
Link to comment
Share on other sites

if $reg_errors is not empty, how about displaying what errors were detected and are stored in it?

if $reg_errors is not empty, then the regex validation fails and the variable $reg_errors is displayed for the affected field for example in the last_name field i would have this: 

 <?php if(isset($reg_errors['last_name'])) echo $reg_errors['last_name'];?>

.

 

I am using a flag $OK, which is a Boolean variable that is initialized to either true or false and i am using it to check whether my query has

happened. For instance, i set $OK to false and reset to true only when a database query executes successfully,

// what to do if successful or show me an error
  if ($OK) {
	echo 'success';
	exit;
  } else {
	$error = $stmt->error;
  }

I am then using this to to display the error message if the insert operation fails: but i am not getting any errors even though the insert query is obviously failing.

<?php if (isset($error)) {
echo "<p>Error: $error</p>";
} ?>
Link to comment
Share on other sites

What is $_POST['donor_id']?  There does not appear to be field with that name in your form.

 

If it is an auto-increment field then you do not need to reference it in the insert query. You only need to reference the fields you're adding the values to.

Thanks, i have removed that.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.