0

I am Using a form to insert data into a table i have created, I receive no errors but for some reason, it states it worked, although it does not insert any data, Here is what I'm using:
this is the form page:

<head>
	<link rel="stylesheet" href="../uploads/css/style.css"  type="text/css" />
</head>
<body>
<form method="post" action="_update_db.php">

Name:<br />

<input type="text" name="Name" size="11" /><br />

Title:<br />

<input type="text" name="Title" size="30" /><br />

Description:<br />

<input type="text" name="Description" size="50" /><br />

<input type="submit" value="Update Database" />

</form>
</body>

Here is the PHP script:

<head>
	<link rel="stylesheet" href="../uploads/css/style.css"  type="text/css" />
</head>
<body>
<?php
$Name = $_POST['Name'];
$Title = $_POST['Title'];
$Description = $_POST['Description'];

mysql_connect ("xxxx", "xxxx", "xxxxx") or die ('I cannot connect to the datebase because: ' . mysql_error());
mysql_select_db ("xxxxxx");

$query="INSERT INTO game_pages (ID, Name, Title, Discription)VALUES ('NULL','".$Name."', '".$Title."', '".$Description."')";

echo "Database Updated With: <br />Name: " .$Name. " <br />Title: ".$Title." <br />Description: ".$Description ;

?>
</body>

Any Ideas, thanks in advance.

5
Contributors
6
Replies
41
Views
5 Years
Discussion Span
Last Post by shafakhat91
0

use die(mysql_error()) next to mysql_query(). Due to this you can check your query runs correctly or not. Before insert values into ddatabase, just echo the values wheather it display or not. If it displayed, then you need to find error on your query

0

HOLY SYNTAX ERRORS BATMAN!

Your HTML is fine but the PHP needs some fine-tuning. I'll try my best to go through a few things one by one and provide solutions as I go. This might end up being the longest post I'll ever write on Daniweb. Lol

it states it worked, although it does not insert any data

Your echo statement that says "Database Updated With" will run no matter what because it's not wrapped in a conditional statement to check whether the database actually gets updated or not

Instead of ...

$query="INSERT INTO game_pages (ID, Name, Title, Discription)VALUES ('NULL','".$Name."', '".$Title."', '".$Description."')";

echo "Database Updated With: <br />Name: " .$Name. " <br />Title: ".$Title." <br />Description: ".$Description ;

... you could have written...

echo "THE BROCCOLI MUST DIE!!";

echo "Database Updated With: <br />Name: " .$Name. " <br />Title: ".$Title." <br />Description: ".$Description ;

... and it would still say "Database Updated With:" bla bla bla

But, before we even get to that, after you build your query, you have to use mysql_query to actually run that query. Also, for a better understanding, do a little reading up on what NULL means in SQL. In short, NULL is not a value, but rather THE LACK OF a value. You're passing NULL as a string into an your ID. Your ID field should be setup as an AUTO_INCREMENT PRIMARY_KEY so make sure of that first with whatever MySQL Admin app you're using, and then you don't need to pass anything to it.

Then you can do this:

$query = "INSERT INTO game_pages (Name, Title, Discription)VALUES ('".$Name."', '".$Title."', '".$Description."')";
$result = mysql_query($query);

After you have that, you can check for the "result" to see if everything went through fine and dandy, and include your echo statement inside a conditional like this:

if($result) {
    echo "Database Updated With: <br />Name: " .$Name. " <br />Title: ".$Title." <br />Description: ".$Description."";

    // REMEMBER TO INCLUDE THE .""; AT THE END OF YOUR STATEMENT
    // EVERY STATEMENT NEEDS TO CLOSE PROPERLY
    // YOUR ORIGINAL STATEMENT DOES NOT DO THIS

} else {
    echo "Something's not right.  Nothing was inserted";
}

To finish off, I will say this. It's obvious you are new to PHP. so make sure this is not a public applicaton. In your PHP script, you have no defense against sql injection, and neither are you checking for valid values before DB insertion. Breaking into something like this would be very easy.

Hope this helps to start you off.

Cheers!

0
 $sqldel = "DELETE FROM saveproposal WHERE PI='$peru' AND RTitle='$title' AND Email='$email'";
 $resdel = mysqli_query($con, $sqldel);   

$sqls = "INSERT INTO saveproposal (ID, PI, Email, RTitle, Coauthors, ExSummary, LReview, Objective, Methodology, EOutput, References, RDuration) VALUES (NULL, '$peru', '$email', '$title', '$auth', '$eta', '$pta', '$ota', '$mta','$eota','$rta','$rdt');";


                    if($result)
                    { 
                            header("Location:http://mdskhan.netau.net/subdone.php");
                    }else{
                                $erro ="$peru,$email,$title,$auth,$eta,$pta,$ota,$mta,$eota,$rta,$rdt";

                                       echo $erro;

                                       echo "SQL Query to execute: $sqls";

                                       die('Invalid query: ' . mysql_error());
                    }

All php variables contains text...

Here is the Output:

Mohammed Shafakhatullah Khan,shafakhat@unizwa.edu.om,One,Nine,Two,Three,Four,Five,Six,Seven,EightSQL Query to execute: INSERT INTO saveproposal (ID, PI, Email, RTitle, Coauthors, ExSummary, LReview, Objective, Methodology, EOutput, References, RDuration) VALUES (NULL, 'Mohammed Shafakhatullah Khan', 'shafakhat@unizwa.edu.om', 'One', 'Nine', 'Two', 'Three', 'Four', 'Five','Six','Seven','Eight');Invalid query:

Every thing is nice and good but i am not getting the data in the table i don't know where is the mistake in INSERT .... Please help me out!!!
0
 $sqldel = "DELETE FROM saveproposal WHERE PI='$peru' AND RTitle='$title' AND Email='$email'";
 $resdel = mysqli_query($con, $sqldel);   

$sqls = "INSERT INTO saveproposal (ID, PI, Email, RTitle, Coauthors, ExSummary, LReview, Objective, Methodology, EOutput, References, RDuration) VALUES (NULL, '$peru', '$email', '$title', '$auth', '$eta', '$pta', '$ota', '$mta','$eota','$rta','$rdt');";
$result = mysqli_query($con,$sqls);

if($result)
{ 
     header("Location:http://mdskhan.netau.net/subdone.php");
}else{
        $erro ="$peru,$email,$title,$auth,$eta,$pta,$ota,$mta,$eota,$rta,$rdt";

        echo "Data is:".$erro;

        echo "SQL Query to execute: $sqls";

        die('Invalid query: ' . mysql_error());
}

PROBLEM: INSERT QUERY IS NOT WORKING....

OUTPUT:

Data is: Mohammed Shafakhatullah Khan,shafakhat@unizwa.edu.om,One,Nine,Two,Three,Four,Five,Six,Seven,Eight
SQL Query to execute: INSERT INTO saveproposal (ID, PI, Email, RTitle, Coauthors, ExSummary, LReview, Objective, Methodology, EOutput, References, RDuration) VALUES (NULL, 'Mohammed Shafakhatullah Khan', 'shafakhat@unizwa.edu.om', 'One', 'Nine', 'Two', 'Three', 'Four', 'Five','Six','Seven','Eight');
Invalid query:

Please help me out!!!!!! I don't understand why INSERT QUERY IS NOT WORKING
0

Here is the table i created....

CREATE TABLE  `a1878876_uonrm`.`saveproposal` (
`ID` INT( 20 ) NOT NULL AUTO_INCREMENT ,
`PI` VARCHAR( 500 ) NOT NULL ,
`Email` VARCHAR( 200 ) NOT NULL ,
`RTitle` VARCHAR( 500 ) NOT NULL ,
`Coauthors` VARCHAR( 500 ) NOT NULL ,
`ExSummary` VARCHAR( 500 ) NOT NULL ,
`LReview` VARCHAR( 500 ) NOT NULL ,
`Objective` VARCHAR( 500 ) NOT NULL ,
`Methodology` VARCHAR( 500 ) NOT NULL ,
`EOutput` VARCHAR( 500 ) NOT NULL ,
`References` VARCHAR( 500 ) NOT NULL ,
`RDuration` VARCHAR( 500 ) NOT NULL ,
PRIMARY KEY (  `ID` )
) ENGINE = MYISAM COMMENT =  'This Table save the proposals'
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.