Jump to content

[SOLVED] php $_SERVER["PHP_SELF"] and mySQL question


NLCGraphics

Recommended Posts

Hi,

 

VER: PHP5.2.2 and mySql 5.0.45

 

I've been learning mySql and have a book and everything. Yep amazing but the question lies within the code of one of the tutorials.

 

Making a newsletter subscription page.

The problem is when I submit my form it comes up as an HTTP 500 Internal Server Error.

 

I have a feeling it has to do with this part of the code somehow:

$display_block="

<form method=\"POST\" action =\"".$_SERVER["PHP_SELF"]."\">

 

 

I don't like posting long code but I don't know where I'm going wrong, please take a look and see if everything looks ok, I copied it right from the book.

 

 

** EDIT: View issue here http://www.skywardz.com/manage.php

Thanks,

NLCGraphics

 

<?php

//set up a couple of functions
function doDB(){
global $mysqli;

//connect to server and select database; you may need it
$mysqli = mysqli("mavedog21.startlogicmysql.com","sky77",
"eman","testdb");

// if connection fail, stop script execution
if(mysqli_connect_errno()){
printf("Connect failed: %s\n", mysqli_connect_error());
exit();
}
}

function emailChecker ($email){

global $mysqli, $check_res;

//check that email is not already in list
$check_sql = "SELECT id FROM subscribers WHERE email ='".$email."'";
$check_res = mysqli_query ($mysqli, $check_sql) or die (mysqli_error($mysqli));

}

//determine if they need to see the form or no
if(!$_POST){
//they need to see the form, so create form block
$display_block="
<form method=\"POST\" action =\"".$_SERVER["PHP_SELF"]."\">
<p><strong>Your Email Address:</strong><br/>
<input type=\"text\" name=\"email\" size=\"40\"></p>

<p><strong> Action:</strong><br/>
Subscribe: <input name=\"action\" type=\"radio\"
value=\"sub\" checked />
Unsubscribe: <input name=\"action\" type=\"radio\"
value=\"unsub\">

<p><input type=\"submit\" name=\"submit\" value=\"Submit Form\"></p>
</form>";

}else if ($_POST["action"]=="sub"){
//trying to subscribe; validate emil address
if ($_POST["email"]==""){
header("Location: manage.php");

}else{
//connect to database
doDB();

//checkthat email is in list
emailChecker($_POST["email"]);

//get number of results and do action
if (mysqli_num_rows($check_res) < 1){
//free result
mysqli_free_result($check_res);

//add record
$add_sql="INSERT INTO subscribers (email) VALUES('".$_POST["email"]."')";

$add_res= mysqli_query($mysqli, $add_sql) or die (mysqli_error($mysqli));

$display_block= "<p> Thank you for signing up</p>";

//close connection to MySql
mysqli_close($mysqli);
}else{
//print failure message
$display_block="<p>You're already subscribed</p>";
}
}
}else if (($_POST) && ($_POST["action"]=="unsub")){

//trying to unsubscribe; validate email address
if ($_POST["email"]==""){
header("Location: manage.php");
exit;
}else{
//connect to database
doDB();
$display_block = $_POST["email"];
//checkthat email is in list
emailChecker($_POST["email"]);

//get number of results and do action
if (mysqli_num_rows($check_res) < 1){
//free result
mysqli_free_result($check_res);

//print failure message
//$display_block="<p>Couldn't find your address!</p> No action was taken.</p>";
}else{
//get value of ID from result
while($row=mysqli_fetch_array($check_res)){
$id=$row["id"];
}

//unsubscribe the address
$del_sql="DELETE FROM subscribers
WHERE id='".$id."'";

$del_res=mysqli_query($mysqli, $del_sql) or die (mysqli_error($mysqli));
$display_block= "<p> You're now unsubscribed.</p>";
}
mysqli_close($mysqli);
}
}
?>

When I use single quotes inside the double quotes the page does not even appear. I get an automatic error.

 

I've tried a bunch of different ways with the quotes. this way so far works the best. At least it displays the page. The other wasy seem to display a blank page when loading.

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.