PDA

View Full Version : + REP - PHP Help



adamFTW
06-12-2007, 12:30 AM
I have the following code, but the info submitted doesn't submit to the database.

Any help?


<?php
// The database
include 'config.php';

// The variables
$name = ($_POST["name"]);
$grade = ($_POST["grade"]);
$comment = ($_POST["comment"]);
$ip = $_SERVER[REMOTE_ADDR];

//Those horrible IF's - Form data
if ($_GET["action"] == "submit")
{
if ($name == "")
{
echo "Whoops! You didn't enter your name.";
exit;
}


if ($comment== "")
{
echo "Whoops! You didn't enter a comment.";
exit;
}
$name = addslashes(htmlspecialchars($name));
$grade = addslashes(htmlspecialchars($grade));
$comment = addslashes(htmlspecialchars($comment));
$addpetition = mysql_query("INSERT INTO `petition` (`name`, `grade`, `comment`, `ip`) VALUES ('$name','$grade','$comment', '$ip')");

// The form details are fine, display text & details
echo "XX: <br />
Name: $name<br />
Grade: $grade<br />
Your comment: $comment<br />
<br />
To view all the signatures, click <a href=\"petition.php?view=entries\">here.</a> To go back to the home page, click <a href=\"index.php\">here.</a>";
exit;
}

else
// Display the form
{
echo "
<div align\"center\">XX</div><br />
<br />
<form action=\"?action=submit\" method=\"post\">
<div id=\"title\">Name:</div>
<input type=\"text\" name=\"name\"><br /><br />
<div id=\"title\">XX:</div>
<select name=\"XX\">
<option id=\"5\">XX</option>
<option id=\"5\">XX</option>
<option id=\"6\">XX</option>
<option id=\"7\">XX</option>
<option id=\"8\">XX</option>
</select><br /><br />
<div id=\"title\">Comment:</div>
<style=\"width=\"169\"; height=\"116\"><textarea rows=\"7\" input type=\"text\" name=\"comment\" cols=\"28\"></textarea><br />
<input type=\"submit\" name=\"submit\" value=\"Sign the petition!\">
</form>";
exit;
}

?>

Dentafrice,
06-12-2007, 12:32 AM
<?php
// The database
include 'config.php';

// The variables
$name = ($_POST["name"]);
$grade = ($_POST["grade"]);
$comment = ($_POST["comment"]);
$ip = $_SERVER[REMOTE_ADDR];

//Those horrible IF's - Form data
if ($_GET["action"] == "submit") {
if ($name == "") {
echo "Whoops! You didn't enter your name.";
exit;
}


if ($comment == "") {
echo "Whoops! You didn't enter a comment.";
exit;
}
$name = addslashes(htmlspecialchars($name));
$grade = addslashes(htmlspecialchars($grade));
$comment = addslashes(htmlspecialchars($comment));
$addpetition = mysql_query("INSERT INTO `petition` (`name`, `grade`, `comment`, `ip`) VALUES ('$name','$grade','$comment', '$ip')") or die(mysql_error());

// The form details are fine, display text & details
echo "XX: <br />
Name: $name<br />
Grade: $grade<br />
Your comment: $comment<br />
<br />
To view all the signatures, click <a href=\"petition.php?view=entries\">here.</a> To go back to the home page, click <a href=\"index.php\">here.</a>";
exit;
} else // Display the form
{
echo "
<div align\"center\">XX</div><br />
<br />
<form action=\"?action=submit\" method=\"post\">
<div id=\"title\">Name:</div>
<input type=\"text\" name=\"name\"><br /><br />
<div id=\"title\">XX:</div>
<select name=\"XX\">
<option id=\"5\">XX</option>
<option id=\"5\">XX</option>
<option id=\"6\">XX</option>
<option id=\"7\">XX</option>
<option id=\"8\">XX</option>
</select><br /><br />
<div id=\"title\">Comment:</div>
<style=\"width=\"169\"; height=\"116\"><textarea rows=\"7\" input type=\"text\" name=\"comment\" cols=\"28\"></textarea><br />
<input type=\"submit\" name=\"submit\" value=\"Sign the petition!\">
</form>";
exit;
}
?>


Should output a MySQL error.

adamFTW
06-12-2007, 12:43 AM
Did you edit the code?

And don't bother - I fixed it.

I have one more question, what is wrong with this code, it's suppose to display the data from a mysql database but it doesn't.

<?php

include "config.php";

$sql = "SELECT * FROM petition";
$check = mysql_query($connect, $sql)
// The above variables will be used to check our connection to MySQL.

if ($check) { // If we're able to connect, we continue in displaying.

while ($display = mysql_fetch_array($check, MYSQL_ASSOC)) {

$name = $display['name'];
$grade = $display['grade'];
$comment = $display['comment'];

// Above are variables defined for our Name, XX and Comment.

echo "
Name: $name
<br>
<br>
XX: $XX
<br>
<br>
Comment: <br><br> $comment
<br>
___________________
";

} else {

echo("Sorry. We could not retrieve the information requested. Please try again later");
}

Dentafrice,
06-12-2007, 12:47 AM
<?php

include "config.php";

$sql = "SELECT * FROM petition";
$check = mysql_query("SELECT name, grade, comment FROM petition") or die("Cannot get it.");
// The above variables will be used to check our connection to MySQL.

while ($display = mysql_fetch_array($check)) {

$name = $display['name'];
$grade = $display['grade'];
$comment = $display['comment'];

// Above are variables defined for our Name, XX and Comment.

echo "
Name: $name
<br>
<br>
XX: $XX
<br>
<br>
Comment: <br><br> $comment
<br>
___________________
";

}
?>

Beau
06-12-2007, 12:51 AM
I think your PHP configuration for MySQL is preventing it from generating errors...

Dentafrice,
06-12-2007, 12:52 AM
The PHP configuration is probably preventing the whole script from generating errors, place this at the top of your file, see what it gives you on load.



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


before anything.

adamFTW
06-12-2007, 12:58 AM
The code you posted earlier works fine.

When I add that display_errors code to the top of my code, I get: Notice: Use of undefined constant xx_who - assumed 'xx_who' in /home/voonu/public_html/miss/config.php on line 3

Dentafrice,
06-12-2007, 01:01 AM
Yeah, remove it now. That just displays everything. If you just want to display errors use E_ERROR

adamFTW
06-12-2007, 01:06 AM
Okay, thank you.

Want to hide these adverts? Register an account for free!