Jump to content

Insert Is Not Inserting Rows In Table.


tahakirmani

Recommended Posts

Hi,

 

I am writing a PHP code in which i am inserting a row in a table from users input. I am using the following code. Code runs successful and it prints the line 'Account Created', but nothing happens to the table. Records dont add there. When i write the same MYSQL code from phpmyAdmin, it runs successful and add the records on table. Kindly tell me the issue in the following code.

 

Thanks,

Taha

 

<?php

if(isset($_POST['name']) && isset($_POST['password']) && isset($_POST['email_address'])) {
$name= $_POST['name'];
$email_address= $_POST['password'];
$password= $_POST['email_address'];

if(!empty($name) && !empty($email_address) && !empty($password)) {


 echo $query= "INSERT INTO `a_database`.`email` (`id`, `name`, `email_address`, `password`) VALUES (' ', '$name', '$email_address', '$password')";

$query_run= mysql_query($query);

echo "Account Created";

Link to comment
https://forums.phpfreaks.com/topic/271396-insert-is-not-inserting-rows-in-table/
Share on other sites

I think you have these backwards:

$email_address= $_POST['password'];
$password= $_POST['email_address'];

 

It should be this:

 

$email_address= $_POST['email_address'];
$password= $_POST['password'];

mysql is neither giving any error message not inserting any row in the table. The following output appears.

 

That's probably because you aren't checking for those errors!

if(!isset($_POST['name']) || !isset($_POST['password']) || !isset($_POST['email_address']))
{
   echo "Error: Required fields are missing.";
}
else
{
   $name		  = mysql_real_escape_string(trim($_POST['name']));
   $email_address = mysql_real_escape_string(trim($_POST['email_address']));
   $password	  = mysql_real_escape_string(trim($_POST['password']));

   if(!empty($name) && !empty($email_address) && !empty($password))
   {
    $query= "INSERT INTO `a_database`.`email`
				 (`name`, `email_address`, `password`)
			 VALUES
				 ('$name', '$email_address', '$password')";
    $result = mysql_query($query);

    if($result)
    {
	    echo "Account Created";
    }
    else
    {
	    echo "Error running script<br><br>Query: {$query}<br>Error: " . mysql_error();
    }
   }
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.