Forum Moderators: coopster

Message Too Old, No Replies

Data won't post

         

outdoorxtreme1

11:08 pm on Dec 13, 2005 (gmt 0)

10+ Year Member



Could someone tell me what is wrong with my code. My data wont post to my SQL table.

<html>
<head>
<title>Add News</title>
<meta http-equiv="Content-Type" content="text/html; charset="iso"-8859-1">
</head>
<body>
<?
if(isset($add_n)){
$link = @mysql_connect(localhost, username, password);
if(!$link){
echo('Error connecting to the database: ' . $mysql_error());
exit();
}
$db = @mysql_selectdb('mydatabase');
if(!$db){
echo('Error selecting database: ' . $mysql_error());
exit();
}
$query = "INSERT INTO news(name, email, headline, story, timestamp)VALUES('$name', '$email', '$headline', '$story', NOW())";
$result = @mysql_query($query);
if(!$result){
echo('Error adding news: ' . $mysql_error());
exit();
}else{
mysql_close($link);
echo('Success!<br><a href="add.php">Click here</a> to add more news.<br><a href="edit.php">Click here</a> to edit news.<br><a href="../index.php">Click here</a> to return to the main page.');
}
}else{
?>
<form name="form1" method="post" action="<? echo $PHP_SELF;?>">
<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%">Name</td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td>Email</td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Headline</td>
<td><input name="headline" type="text" id="headline"></td>
</tr>
<tr>
<td>News Story</td>
<td><textarea name="story" id="story"></textarea></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input name="hiddenField" type="hidden" value="add_n">
<input name="add" type="submit" id="add" value="Submit">
</div></td>
</tr>
</table>
</form>
<? }?>
</body>
</html>

mm1220

11:57 pm on Dec 13, 2005 (gmt 0)

10+ Year Member



The values from the form will be posted to the php script in this form:
$_POST['add_n'] and $_POST['name'] etc etc

They will only show up directly as $add_n or $name if php is configured on your server with Register_Globals turned on. (This generally isn't the case as it presents a security risk.)

dreamcatcher

2:34 am on Dec 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yep, its as mm1220 has mentioned, you need to access your variables using the superglobal $_POST. Register globals will be off on your server. It is for security, even though a lot of shared servers still have it set to on.

dc

sunveria

11:24 am on Dec 17, 2005 (gmt 0)

10+ Year Member



Try this

<html>
<head>
<title>Add News</title>
<meta http-equiv="Content-Type" content="text/html; charset="iso"-8859-1">
</head>
<body>
<?
if(isset($_POST['hiddenField'])){
$link = mysql_connect(localhost, username, password) or die('Error connecting to the database: ' . $mysql_error());
$db = mysql_select_db('mydatabase') or die('Error selecting database: ' . $mysql_error());
$name=$_POST['name'];
$email=$_POST['email'];
$headline=$_POST['headline'];
$story=$_POST['story'];
$query = "INSERT INTO news(name, email, headline, story, timestamp) VALUES ('$name', '$email', '$headline', '$story', NOW())";
$result = mysql_query($query) or die('Error adding news: ' . $mysql_error());
mysql_close($link);
echo('Success!<br><a href="add.php">Click here</a> to add more news.<br><a href="edit.php">Click here</a> to edit news.<br><a href="../index.php">Click here</a> to return to the main page.');
}
}else{
?>
<form name="form1" method="post" action="<? echo $PHP_SELF;?>">
<table width="50%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="50%">Name</td>
<td><input name="name" type="text" id="name"></td>
</tr>
<tr>
<td>Email</td>
<td><input name="email" type="text" id="email"></td>
</tr>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Headline</td>
<td><input name="headline" type="text" id="headline"></td>
</tr>
<tr>
<td>News Story</td>
<td><textarea name="story" id="story"></textarea></td>
</tr>
<tr>
<td colspan="2"><div align="center">
<input name="hiddenField" type="hidden" value="add_n">
<input name="add" type="submit" id="add" value="Submit">
</div></td>
</tr>
</table>
</form>
<? }?>
</body>
</html>

^ ^