Jump to content

MYSQL INSERT Error

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
2 replies to this topic

#1
dunila

dunila

    Newbie

  • Members
  • Pip
  • 3 posts
I learn to create a web with using a global variable for database.
When i create registration form, it was fail.

global.php

Quote

<?php
$db_location = "localhost";
$db_name = "php_learn";
$db_prefix = "pl_";
$db_user = "root";
$db_password = "";
?>

register.php

Quote

<?php
require ('global.php');

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


if($name == '' && $email == ''){ echo '<p class="warn">Input your name & Email !</p>' ; exit(); }
if ($name == ''){ echo '<p class="warn">Input your name !</p>' ; exit(); }
if ($email == ''){ echo '<p class="warn">Input your email !</p>' ; exit(); }

if (!ereg("^.+@.+\\..+$",$email)){ echo '<p class="warn">Your email address is invalid !' ; exit(); }

$db_user = $db_prefix.'user' ;
$connection = mysql_connect("$db_location","$db_user","$db_password");
if (!$connection){
die('Connection to Database Failed :' . mysql_error());
}
mysql_select_db("$db_name",$connection);
$data = "INSERT INTO'.$db_user.'(name, email) VALUES ('$name','$email')";
$process_insert = mysql_query($data, $connection);

if(!$process_insert){
echo '<p class="warn">Registration failed, please try again or contact administrator !' ; exit();
}else {
echo 'Thanks to register.';
}

?>
This (register.php) not work, ti was give me "Registration failed, please try again or contact administrator !" .

What's the problem ??

But, if i am using this code.
Nothing false.

Quote

mysql_connect("localhost","root","");
mysql_select_db("php_learn");

$input = "INSERT INTO pl_user(name, email) VALUE('$name','$email') ";
$result = mysql_query($input);

if(!$input){
echo "Error!";
} else{
echo "Clear";
}

Thanks for help.

#2
alilg

alilg

    Learning Programmer

  • Members
  • PipPipPip
  • 56 posts
$data = "INSERT INTO '.$db_user.'(name, email) VALUES ('$name','$email')";

'.$db_user.' is not name of the table which you users information saved to.

$input = "INSERT INTO pl_user(name, email) VALUE('$name','$email') ";

pl_user is right, not $db_user!

$db_user is the USERNAME of your DATABASE!

#3
dunila

dunila

    Newbie

  • Members
  • Pip
  • 3 posts
@alilg
thanks, trying your help :)