0

index.html:

<!DOCTYPE html>
<html lang="en"> 
<head>

<body>

<form id="myform" action="userinfo.php" method="post" >

Name:   <input type="test" name="name"  autofocus>

Age:    <input type="text" name="age"  >
    <button id="sub"> save</button>

 </form>

 <span id="result"></span>

  <script src="script/jquery-1.8.1.min.js" type="text/javascript"></script>
 <script src="script/my_script.js" type="text/javascript"></script>
 </body>
 </html>

my_script.js:

$("#sub").click( function() {
$.post( $("#myform").attr("action"), 
     $("#myform :input").serializeArray(), 
     function(info){ $("#result").html(info); 
});
clearInput();
});

$("#myform").submit( function() {
  return false; 
  });

function clearInput() {
$("#myform :input").each( function() {
   $(this).val('');
});

db.php:

<?php

$conn = mysql_connect('localhost','B00556019','73eKESV3') ; 
$db = mysql_select_db('b00556019');

?>

userinfo.php:

<?php

    include_once('db.php');

    $name = $_POST['name'];
    $age = $_POST['age'];

    if(mysql_query("INSERT INTO user (name, age) VALUES (
                   '$name','$age')";))
        echo "Successful";
    else
        echo "insertion failed";

?>

It won't post to database

database name: b00556019
table: user
fields: name(varchar, 15). age(INT, 3)

nothing happens apart form the userinfo page is displayed as blank.

Anybody with some advice in how to post to the database would be great and also if someone has an easy tutorial as this was a basic tutorial but still didn't work.

  • Take the semicolon out the if statement and see if that resolves it. – Daniel Mar 11 '14 at 17:21
  • You need to reference $conn in the mysql_query – Steven Martin Mar 11 '14 at 17:23
  • PHP does not connect to phpMyAdmin. phpMyAdmin is not a database. MySQL is the database. – deceze Mar 11 '14 at 17:30
  • If you're doing this based on a tutorial, you need to stop reading that tutorial IMMEDIATELY. The `mysql_*` functions have been [deprecated](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php). Instead, you should be using [mysqli](http://us2.php.net/manual/en/book.mysqli.php) or [PDO](http://us2.php.net/manual/en/book.pdo.php) – Patrick Q Mar 11 '14 at 17:43

2 Answers2

1

Sidenote: As it stands, you are open to SQL injection

Do consider switching to using mysqli_* functions along with prepared statements or PDO. mysql_* functions are deprecated and will be deleted from future PHP releases.

Instead of:

if(mysql_query("INSERT INTO user (name, age) VALUES (
               '$name','$age')";))
    echo "Successful";
else
    echo "insertion failed";

Use:

$name = mysql_real_escape_string($_POST['name']);
$age = mysql_real_escape_string($_POST['age']);

$result = mysql_query("INSERT INTO user (name, age) VALUES ('$name','$age')");
    if(!mysql_query($result)){ 
        echo "Insertion was not successful."; 
} else {
    echo "Insertion was successful.";
}

You may also need to pass the DB connection:

if(!mysql_query($result,$conn))

Prepared statements method:

$conn = new mysqli("xxx", "xxx", "xxx", "xxx");

/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}

$name = $_POST['name'];
$age = $_POST['age'];

$result = $conn->prepare("INSERT INTO user (name, age) VALUES (?,?)");
$result->bind_param("ss", $name, $age);
$result->execute();
$result->close();

// If there is any error with your SQL statement an
// error is thrown and displayed to the user:
printf("Prepared Statement Error: %s\n", $conn->error);
Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

first of all correct the table name : "table: users" and it is "user" you used in code.

if(!$conn){
     echo mysql_error() ; // first check while connecting..
}

// on the insertion code ..... 

if(mysql_query("INSERT INTO user (name, age) VALUES (
               '$name','$age')";))
    echo "Successful";
else
    echo mysql_error($conn) ; // you will see what is the error..
    echo "insertion failed";
Riad
  • 3,822
  • 5
  • 28
  • 39