1

I have a form and trying to insert data to the mysql database. but it always jump into the error.

Same database connection working fine to view data already in the database.

Database Connection for the page stored in a separate file :

   <?php 
$host ="localhost";
$user = "CENSORED";
$password = "CENSORED";



$link = mysql_connect($host,$user,$password) or die("An error occurred while connecting...");

//Database Selection
$dbname="CENSORED";
mysql_select_db($dbname);

?>

HTML Form

<form action="add_admin.php" method="post">
    <table>
        <tr>
        <td>Email Address :</td>
        <td><input id="admin_email" name="admin_email" type="text" size="20"</></td>
        </tr>
        <tr>
        <td>Name :</td>
        <td><input id="admin_name" name="admin_name" type="text" size="20"</></td>
        </tr>
        <tr>
        <td>Mobile :</td>
        <td><input id="admin_mobile" name="admin_mobile" type="text" size="12"</></td>
        </tr>
        <tr>
        <td>Address :</td>
        <td><textarea id="admin_address" name="admin_address" rows="4" cols="50"/> </textarea></td>
        </tr>
        <td>Password :</td>
        <td><input id="admin_pw" name="admin_pw" type="text" size="20"</></td>
        </tr>
        <td><input type="reset" value="Reset"></td>
        <td><input type="submit" value="Submit"></td>
        </tr>
    </table>
    </form>

PHP Code

    <?php
$admin_email=$_POST['admin_email'];
$admin_name=$_POST['admin_name'];
$admin_mobile=$_POST['admin_mobile'];
$admin_address=$_POST['admin_address'];
$admin_password=$_POST['admin_password'];

$sql = "INSERT INTO admin (admin_email,admin_name,admin_mobile,admin_address,admin_password) VALUES ('$admin_email','$admin_name','$admin_mobile','$admin_address','$admin_password')";

if( mysql_query($link,$sql))
    {
        echo "Records Added";
    }
    else
    {
        echo "ERROR";
        mysql_error($link);
    }

mysql_close($link);

?>

Thanks in advance.

7
  • You should not do any of this. You are opening yourself up to sql injection. Switch to PDO or mysqli_ with bound parameters Commented Oct 22, 2015 at 7:32
  • mysql_query() is deprecated. you should either use mysqli or PDO Commented Oct 22, 2015 at 7:33
  • 1
    echo $sql; and run on phpmyadmin Commented Oct 22, 2015 at 7:33
  • 1
    Also, what is the error you are referring to? Commented Oct 22, 2015 at 7:33
  • its displaying this error echo "ERROR"; on the php page Commented Oct 22, 2015 at 7:39

6 Answers 6

1

you have to include your Database connection file which you have kept as separate file in your php file.

<?php
    include("dbconnection filename.php"):// this line.
    $admin_email=$_POST['admin_email'];
    $admin_name=$_POST['admin_name'];
    $admin_mobile=$_POST['admin_mobile'];
    $admin_address=$_POST['admin_address'];
    $admin_password=$_POST['admin_password'];

    $sql = "INSERT INTO admin (admin_email,admin_name,admin_mobile,admin_address,admin_password) VALUES ('$admin_email','$admin_name','$admin_mobile','$admin_address','$admin_password')";

    if( mysql_query($link,$sql))
        {
            echo "Records Added";
        }
        else
        {
            echo "ERROR";
            mysql_error($link);
        }

    mysql_close($link);

?>
Sign up to request clarification or add additional context in comments.

5 Comments

Hmmm... This could be the problem. Nice catch
I have declered the external php file in the header part.
But that should come in the php file where you are inserting the data.
or show us what you have done in header ?? and also please post the error as well.
I changed as you mentioned still getting the Error. Error is the echo "ERROR" message in the else part of if condition
0

Change to this

$sql = "INSERT INTO admin (admin_email,admin_name,admin_mobile,admin_address,admin_password) VALUES ('".$admin_email."','".$admin_name."','".$admin_mobile."','".$admin_address."','".$admin_password."')";

3 Comments

@Rasclatt Not sure if it is php bug or something, I tend to receive insert error like what he/she did here but after implement this, it works.
Error is the echo "ERROR" message in the else part of if condition
@CJM what about the MYSQL error message? It shouldn't just echo ERROR
0

use mysql_real_escape_string

$admin_email=mysql_real_escape_string($_POST['admin_email']);
$admin_name=mysql_real_escape_string($_POST['admin_name']);
$admin_mobile=mysql_real_escape_string($_POST['admin_mobile']);
$admin_address=mysql_real_escape_string($_POST['admin_address']);
$admin_password=mysql_real_escape_string($_POST['admin_password']);

1 Comment

echo $sql; and run on phpmyadmin @CJM
0

You have problems with connecting to a database. I don't like your approach to of connecting to a database so i'll provide mine approach (which works so far).

Your database config should look like

class DataBaseClass{
public $_host = "localhost";
public $_user = "X32284679";
public $_database = "X32284679";
public $_pass = "X32284679";

function connectToDatabase(){
$conn = new mysqli($this->_host, $this->_user, $this->_pass, $this->_database);
$conn->set_charset("utf8");
return $conn;

if(! $conn) {
    echo "Problems with connecting to database!";
    exit;
    }
}
}

Later on in some other code you use this file like this

require('nameOfFile.php');

$db = new DataBaseClass();
$mysqli=$db->connectToDatabase();
$sql = "INSERT INTO admin (admin_email,admin_name,admin_mobile,admin_address,admin_password) VALUES ('$admin_email','$admin_name','$admin_mobile','$admin_address','$admin_password')";
if($rs = $mysqli->query($sql)) {
    //inserted        
else {
    //not inserted
    $mysqli->close();
}

And so on, try this approach and see if it helps you.

1 Comment

Then check checkupdown.com/status/E500.html to solve your problem. Not a client problem, like we assumed.
0

In your PHP page you should include your connection file:

require_once('yourdbconnection.php');

And change $_POST['admin_password'] to $_POST['admin_pw'] according to your HTML.

HTML

<form action="add_admin.php" method="post">
  <table>
    <tr>
      <td>Email Address :</td>
      <td><input id="admin_email" name="admin_email" type="text" size="20"></td>
    </tr>
    <tr>
      <td>Name :</td>
      <td><input id="admin_name" name="admin_name" type="text" size="20"></td>
    </tr>
    <tr>
      <td>Mobile :</td>
      <td><input id="admin_mobile" name="admin_mobile" type="text" size="12"></td>
    </tr>
    <tr>
      <td>Address :</td>
      <td><textarea id="admin_address" name="admin_address" rows="4" cols="50"> </textarea></td>
    </tr>
    <td>Password :</td>
      <td><input id="admin_pw" name="admin_pw" type="text" size="20"></td>
    </tr>
      <td><input type="reset" value="Reset"></td>
      <td><input type="submit" value="Submit"></td>
    </tr>
  </table>
</form>

PHP

<?php
require_once('yourdbconnection.php');

$admin_email=$_POST['admin_email'];
$admin_name=$_POST['admin_name'];
$admin_mobile=$_POST['admin_mobile'];
$admin_address=$_POST['admin_address'];
$admin_password=$_POST['admin_pw'];

$sql = "INSERT INTO admin (admin_email,admin_name,admin_mobile,admin_address,admin_password) VALUES ('$admin_email','$admin_name','$admin_mobile','$admin_address','$admin_password')";

mysqli_query($link, $sql) or die("Error: " . mysqli_error($link));
mysqli_close($link);
?>

This works for me. If it doesn't for you then:

  • Check if query columns match table columns
  • Check if you are using the right database and the right table
  • Check if you are checking result on the right database and the right table

Hope this helps!

EDIT NOTE: I highly suggest you to switch from mysql to mysqli since mysql is now deprecated.

2 Comments

Everything is in order. it is showing Error now. But not showing what is the error.
Are you sure your connection works then? Make sure you have changed from mysql to mysqli also in the connection file please.
0

As you asked me to help out in one of my previous answers i decided to do some fancy stuff with this code :)

Remember, the db rows need to be named the same as your form name="name" for this to work!

db_connect.php:
$dbhost = ""; // this will ususally be 'localhost', but can sometimes differ
$dbname = ""; // the name of the database that you are going to use for this project
$dbuser = ""; // the username that you created, or were given, to access your database
$dbpass = ""; // the password that you created, or were given, to access your database

$conn = mysql_connect($dbhost, $dbuser, $dbpass) or die('An error occured while connecting to: '. $dbhost.' as: '.$dbuser);
mysql_select_db($dbname, $conn) or die('Sorry, an error occured when selecting the database: '.$dbname);

form.php:

<form action="add_admin.php" method="post">
    <table>
        <tr>
        <td>Email Address :</td>
        <td><input id="admin_email" name="admin_email" type="text" size="20"</></td>
        </tr>
        <tr>
        <td>Name :</td>
        <td><input id="admin_name" name="admin_name" type="text" size="20"</></td>
        </tr>
        <tr>
        <td>Mobile :</td>
        <td><input id="admin_mobile" name="admin_mobile" type="text" size="12"</></td>
        </tr>
        <tr>
        <td>Address :</td>
        <td><textarea id="admin_address" name="admin_address" rows="4" cols="50"/> </textarea></td>
        </tr>
        <td>Password :</td>
        <td><input id="admin_pw" name="admin_pw" type="text" size="20"</></td>
        </tr>
        <td><input type="reset" value="Reset"></td>
        <td><input type="submit" value="Submit"></td>
        </tr>
    </table>
    </form>

add_admin.php:

include 'db_connect.php'; //include connection


//Why add all post thingys when you can do it dynamically ?
$i = count($_POST);
$e = 0;


//Do a foreach loop on all POSTS coming in to this file.. 
foreach($_POST as $Key => $Value){


  //Add commas behind everything :)
  if($e++ < $i - 1){

      //Escaping all the strings:
      $Rows .= mysql_real_escape_string($Key).", ";
      $Values .= "'".mysql_real_escape_string($Value)."', ";
  }


  //if its the last one, dont add a comma behind!
  else{

      //Still escaping all the strings:
      $Rows .= mysql_real_escape_string($Key);
      $Values .= "'".mysql_real_escape_string($Value)."'";
  } 

}//end foreach loop





//Insert etc etc...
$sql = mysql_query("INSERT INTO admin($Rows) VALUES($Values)");

//If successful:
if(mysql_query($conn, $sql)){
    echo "Records added.";
}


//Error ?
else{
    echo "Sorry, an error occured while inserting to: ".$Rows;
    echo "<br/>";
    mysql_error($conn);
}


//Close connection:
mysql_close($conn);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.