Jump to content

Recommended Posts

Hey

 

I have this piece of code which is supposed to just add a guide into a database, but it doesnt ADD it it just reloads the page and doesnt send any error/confirmation... please guide me to where I am wrong

 

Thanks guys

 

<?php include("db.php"); ?>
<html>
<body>
Add Guide
<form action="<?php echo $PHP_SELF ?>" method="post">
<b> Guide Name</b>
<BR />
<input type="text" name="name" size="40" maxlength="80" value="" />
<br />
<b>Link</b>
<BR />
<input type="text" name="link" size="40" maxlength="80" value="" />
<br />
<input type="submit" value="Add Guide" /> <input type="reset" value="Reset" />
</form>

<? 
  if($submit)
  {

$name = $_POST["name"];
$link = $_POST["link"];

$addnews =MYSQL_QUERY("INSERT INTO guides (id,name,link)". "VALUES ('NULL', '$name', '$link')");
echo("Guide on: $name - Added!<br>No errors were encountered!<br>");
echo("<a href=\"guidesoverview.php\">Guides Overview</a> | <a href=\"admin.php\">Back to Admin</a>");  
              }
?>
</body>
</html>

Link to comment
https://forums.phpfreaks.com/topic/48787-adding-guide-to-database/
Share on other sites

Where shall I begin

 

if($submit)

Very very bad

I can not see $submit Anywhere on your code

 

You need to call $_POST["submit"]

Although there are no form fields called submit

 

NEXT

I am guessing ID is a auto_increment field,

You do NOT need add in the query null fields

Nor do you need to break out of PHP String.

 

3rd: MYSQL_QUERY is a function and should to be lower case

 

Last

Please add some security before adding to DB

 

REWRITE

 

<?php 
include("db.php"); 
?>
<html>
<body>
Add Guide
<form action="" method="post">
<b> Guide Name</b>
<BR />
<input type="text" name="name" size="40" maxlength="80" value="" />
<br />
<b>Link</b>
<BR />
<input type="text" name="link" size="40" maxlength="80" value="" />
<br />
<!--  NOTE the name field for the submit button -->
<input type="submit" name="submit_btn" value="Add Guide" /> <input type="reset" value="Reset" />
</form>

<?php
//Try not to use shorthand, rumours saying this will be stopped soon

//This is quick hand on IF statement
//IF submit_btn is found, the form is submitted, label $Submit true, else it is false
$Submit = isset($_POST["submit_btn"]) ? true : false;
//If Submit, same as saying if($Submit == true)


if($Submit){
//Secure the values, save from MySQL Injection
$name = mysql_real_escape_string($_POST["name"]);
$link = mysql_real_escape_string($_POST["link"]);

//Easy to debug
$query = "INSERT INTO guides (name, link) VALUES ('".$name."', '".$link."')";
$result = mysql_query($query);
//If the query failed, result would be false
if($result){
	echo "Guide on: $name - Added!<br>No errors were encountered!<br>\n"
	."<a href='guidesoverview.php'>Guides Overview</a> | <a href='admin.php'>Back to Admin</a>";  
}else{
	echo "There has been an error<br />\n";
}
//Also note IF statements, the END bracket is inline with the statement
//All code for that statement is indented, easier to read.
}
?>
</body>
</html>


This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.