hi i am a junior web designer and this the problem i am having is that i cannot find were what i am doing wrong could someone please correct. as far as i can tell i am doing everything ok. the code which i have made wont do the result. my code is shown below. thanks

<?php
    echo "This is PHP!";

$db_host = "";
$db_username = "";
$db_password = "";
$db_database = "";

$db = mysql_connect ($db_host, $db_username, $db_password);
Mysql_select_db($db_database, $db);

if($db){
echo "Successful1";
}
else {
echo "ERROR1";
}


$subject=$_POST['subject'];
$number=$_POST['number'];
$customer_mail=$_POST['customer_mail'];
$detail=$_POST['detail'];

// Insert data into mysql
$sql="INSERT INTO a7732991_1,Register(subject, number, customer_mail, detail)VALUES('$subject', '$number', '$customer_mail','$detail)";
$result=mysql_query($sql);

if($sql){
echo "Successful2";
}
else {
echo "ERROR2";
}


// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}


else {
echo "ERROR";
}


// close connection
mysql_close();
?>
Member Avatar for LastMitch

@fbutt1289

i am having is that i cannot find were what i am doing wrong could someone please correct. as far as i can tell i am doing everything ok. the code which i have made wont do the result. my code is shown below. thanks

Are you familiar with php?

Like what diafol mention:

Add this small code at the end of line 9 & 27: or die(mysql_error());

What error you got on line 9:

$db = mysql_connect ($db_host, $db_username, $db_password) or die(mysql_error());

What error you got on line 27:

$result=mysql_query($sql) or die(mysql_error());

What I just did above is to see whether you are connected to the db and also to tell whether you query is wrong.

Do you understand how this works now? Debugging a code is the first step of understanding how to fixed an error.

<?php
    echo "This is PHP!";
$db_host = "";  // your host name Eg: Localhost
$db_username = "";  //  your db username if using wamp it would be root
$db_password = "";  // your db password if using wamp it would be left blank
$db_database = "";  // name of db
$db = mysql_connect ($db_host, $db_username, $db_password);
mysql_select_db($db_database, $db);
if($db){
echo "Successful1";
}
else {
echo "ERROR1";
}
$subject=$_POST['subject'];
$number=$_POST['number'];
$customer_mail=$_POST['customer_mail'];
$detail=$_POST['detail'];
// Insert data into mysql
$sql="INSERT INTO Register VALUES ('".$subject."', '".$number."', '".$customer_mail."','".$detail."')";
$result=mysql_query($sql);
if($sql){
echo "Successful2";
}
else {
echo "ERROR2";
}
// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}
else {
echo "ERROR";
}
// close connection
mysql_close();
?>

Try this

First test whether the form was submitted i.e by checking for the existence of $_POST['submit'] (or whatever you name the submit button).

if(isset($_POST['submit'])) {

    // do all the assigning of $_POST to variables here and querying

} else {

    // display the form or redirect to a page with the form here
}

You also can't be sure that all $_POST array elements contain values so you have to test each element. If any particular element has no value (i.e user did not fill-in all the fields in the form) then you have to either provide a default value or handle the error.

// example of providing default value
if(isset($_POST['subject'])) {
    $subject=$_POST['subject'];
} else {
    $subject = '';
}

// example of handling the error
if(isset($_POST['number']) && is_numeric($_POST['number'])) {
    $number=$_POST['number'];
} else {
    header('location:error.php');
}
// do this for each element of a $_POST array
...
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.