Jump to content

[SOLVED] Script not running properly


guidance51x

Recommended Posts

Hello Everyone

This is a basic script for posting to a database. I'm not sure why it doesn't work. Can anyone tell me why?

 

<?php
require('config.php');
if(isset($_GET['id']) == TRUE && isset($_GET['td'])==TRUE){
if(is_numeric($_GET['id'])==FALSE || is_numeric($_GET['td'])==FALSE) {
	$error = 1;
}
if($error == 1){
	header("Location: " . $config_basedir);
}
else{
	$validentry = $_GET['id'];
	$validtd = $_GET['td'];
}
}
else{
$validentry = 0;
}
if($_POST['submit']) {
$db = mysql_connect($dbhost,$dbuser,$dbpassword);
mysql_select_db($dbdatabase, $db);
if(!$db){
	die('FAILED TO OPEN DATABASE:' . mysql_error());
}

$sql = "INSERT INTO todo(id, topic, date, comment) 
		VALUES (" . $validentry . ", '" . $_POST['topic'] .
		"', '" . $_POST['date'] . "', '" . $_POST['comment'] . "');";

$result=mysql_query($sql);
if(!$result){
	die('FAILED TO INPUT INTO DATABASE:' . mysql_error());
}
header("Location: index.php?id=" . $validentry);
}

?>
<center>
**All dates must be inputted in the YYYY-MM-DD format** <br>
<br>
<form action="<?php echo $SCRIPT_NAME . "?id=" . $validentry; ?>" method="post">
<table>

<tr>
<td>Topic</td>
    <td><input type="text" name="topic"></td>
</tr>
<tr>
<td>Date</td>
    <td><input type="text" name="lname"></td>
</tr>
<tr>
<td>Comment</td>
    <td><input type="text" name="address"></td>
</tr>
<tr>
<td></td>
    <td><input type="submit" name="submit" value="Submit Note"></td>
</tr>
</table>
</form>
</center>

Link to comment
Share on other sites

This has worked for me:

<?php 
$topic = $_POST['topic'];
$date = $_POST['date'];
$comment = $_POST['comment'];
sql = "INSERT INTO todo(id, topic, date, comment) 
         VALUES ('$validentry', '$topic', '$date', '$comment')"; ?>

This way the double quotes don't get confused with the single quotes.

Link to comment
Share on other sites

you should really have error_reporting on.

 

would have picked up the extra ; at the end of your query:

 

$sql = "INSERT INTO todo(id, topic, date, comment) 
         VALUES (" . $validentry . ", '" . $_POST['topic'] .
         "', '" . $_POST['date'] . "', '" . $_POST['comment'] . "');"; //;"; is no good;

 

should be:

 

$sql = "INSERT INTO todo(id, topic, date, comment) 
         VALUES (" . $validentry . ", '" . $_POST['topic'] .
         "', '" . $_POST['date'] . "', '" . $_POST['comment'] . "')";

Link to comment
Share on other sites

What does happen in the browser when you submit the form? Does it just redisplay the form or does it redirect to index.php?

 

For debugging purposes, please add the following two lines of code immediately after your first opening <?php tag -

 

ini_set("display_errors", "1");
error_reporting(E_ALL);

 

FYI, the extra semicolon ; on the end of the query statement would normally be used when you execute the query directly against the mysql server, but it is optional when executing a query through php. It is not a php or an sql error.

Link to comment
Share on other sites

When i submit the code it redirects to index.php and looks like nothing ever changed.

 

The first thing i saw was this message on top of my script:

 

Notice: Undefined index: submit in C:\xampp\htdocs\sites\smart\addtodo.php on line 20

 

when i try to submit something, the page 404's with this in the URL box:

 

http://localhost/sites/web/%3Cbr%20/%3E%3Cb%3ENotice%3C/b%3E:%20%20Undefined%20variable:%20SCRIPT_NAME%20in%20%3Cb%3EC:%5Cxampp%5Chtdocs%5Csites%5Csmart%5Caddtodo.php%3C/b%3E%20on%20line%20%3Cb%3E43%3C/b%3E%3Cbr%20/%3E?id=1

 

I'm guessing it has a problem with $SCRIPT_NAME or ?id=1... Any help wit clarification?

Link to comment
Share on other sites

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.