Jump to content

My page seems to miss part of my code.


RichG

Recommended Posts

Hi

 

I am working my way through a php book and it is becoming aware that there may be typo's lol. I have done all the code from my latest bit and actualy managed to go through and sort out all my syntax errors but my page does not work as it should.

 

www.richdgrimes.co.uk/phptest/rate_boss.php if you want to view the page.

 

Here is the code

 

<?php 

//First set the form srtings which will be displayed
//in various cases below

$thisfile = $_SERVER['PHP_SELF']; //Have to set this for heredoc

$reg_form = <<<EOREGFORM
<p>We must ask for your name and email address to ensure that no one votes more than once. We do not associate your personal 		information with your rating</p>
<form method="post" action="$thisfile">
Name: <input type="text" size=25 name="name"><br>
Email <input type="text" size=25 name="email">
<input type="hidden" name="stage" value="register">
<br>
<input type="submit" name="submit" value="submit">
</form>
EOREGFORM;

$rate_form = <<< EORATEFORM
<p>My boss is</p>
<form method="post" action="$thisfile">
<input type="radio" name="rating" value=1>
Driving me to look for a new job.<br>
<input type="radio" name="rating" value=2>
Not the worst. But pretty bad.<br>
<input type="radio" name="rating" value=3>
Just so-so.<br>
<input type="radio" name="rating" value=4>
Pretty good.<br>
<input type="radio" name="rating" value=5>
A pleasure to work with.<br><br>
Boss name: <input type="text" size=25 name="boss"><br>
<input type="hidden" name="stage" value="rate"><br><br>
<input type="submit" name="submit" value="submit">
</form>
EORATEFORM;

if (!$_POST['submit'])
{//O1
//First time. Just show registration form
$message = $reg_form;
}//C1
elseif ($_POST['submit'] == 'submit' && $_POST['stage'] == 'register')
{//O2
//Second time. Show the registration for again on error
//Rating form on successful INSERT

if (!$_POST['name'] || $_POST['name'] == "" || strlen($_POST['name']>30) || !$_POST['email'] || $_POST['email'] == "" || strlen($_POST['email']>30))
{//O3
$message = '<p>There is a problem. Did you enter a name and email address?</p>';
$message .= $reg_form;
}//C3
else
{//O4
//Open connection to the database
mysql_connect('xxxx', 'xxxx', 'xxxx')
or die('failure to connect to database');

mysql_select_db('xxxx');

//Check to see if name and email have been used before
$as_name = addslashes($_POST['name']);
$tr_name = trim($as_name);
$as_email = addslashes($_POST['email']);
$tr_email = trim($as_email);
$query = "SELECT sub_id FROM raters WHERE name = '$tr_name' AND email = '$tr_email'";

$result = mysql_query($query);
if (mysql_num_rows($result) > 0)
{//05
error_log(mysql_error());
$message = 'Someone with this name and password has already rated. If you think a mistake has been made, please email help@example.com,';
}//C5
else
{//O6
//Insert name and email address
$query = "INSERT INTO raters (id, name, email) VALUES(NULL, '$tr_name', '$tr_email')";
$result = mysql_query($query);
if (mysql_affected_rows() == 1)
{//07
$message = $rate_form;
}//C7
else
{//08
error_log(mysql_error());
$message = '<p>Something went wrong with your signup attempt.</p>';
$message .= $reg_form;
}//C8
}//C6
}//C4
}//C2
elseif ($_POST['submit'] == 'submit' && $_POST['stage'] == 'rate')
{//O9
//Third time. Store the rating and boss's name

//Open connection to the database
mysql_connect('xxxx', 'xxxx', 'xxxx')
or die('failure to connect to database');

mysql_select_db('xxxx');

//Insert rating and boss's name
$as_boss = addslashes($_POST['boss']);
$tr_boss = trim($as_boss);
$rating = $_POST['rating'];
$query = "INSERT INTO ratings (id, rating, boss) VALUES (NULL, '$rating', '$tr_boss')";
$result = mysql_query($query);

if (mysql_affected_rows() == 1)
{//O10
$message = '<p>Your rating has been submitted.</p>';
}//C10
else
{//011
error_log(mysql_error());
$message = '<p>Something went wrong with your rating attempt. Please try again.</p>';
$message .= $rate_form;
}//C11
}//C9
?>	



<html>
<head>
</head>
<body>
<table border="0" cellpadding="10" width="100%">
<tr>
<td bgcolor="#f0f8ff" align="center" valign="top" width="17%"></td>
<td bgcolor="#ffffff" align="left" valign="top" width="83%"><?php echo $message; ?></td>
</tr>
</table>
</body>
</html>

 

My problem is happening around block //O2 and //O3 if i leave the fields blank then i get the correct error message. If i  enter an email address it says the problem with sign up attempt error //O8. So then i went to my database and put myself in so when i type my details in it would pick up my details and return the already registered error //O5. It failed to do this as well and went straight to the //O8 error again and did not check the database. As my error did not come up when connecting to the server can anyone see my problem?

 

Cheers

RichG

Link to comment
Share on other sites

Tell me if i am wrong but i have looked at the code again and I have read it as follows

 

$query variable is equal to a database query "INSERT or SELECT" and $result is equal to the result of that query BUT does the query actually run?

 

What line actually tells the code to go and run the query.

 

is this my problem.

 

Cheers

RichG

Link to comment
Share on other sites

OK getting somewhere

 

I can now enter details i know are in the database and get the error someone with those details has already registered with the site.

 

But if i enter new details then they do not get submitted to the DB.

 

Have a look at that part please. I am working away while waiting for feedback.

 

Cheers

RichG

Link to comment
Share on other sites

Hi again

 

Still can't find the problem with this code. It does everything except insert the data to the DB. See section //O6 - //C2 it does not put new data into the database for some reason then i just get the error "Something went wrong with your signup attempt" Can anyone see the problem?

 

<?php 

//First set the form srtings which will be displayed
//in various cases below

$thisfile = $_SERVER['PHP_SELF']; //Have to set this for heredoc

$reg_form = <<<EOREGFORM
<p>We must ask for your name and email address to ensure that no one votes more than once. We do not associate your personal 		information with your rating</p>
<form method="post" action="$thisfile">
Name: <input type="text" size=25 name="name"><br>
Email <input type="text" size=25 name="email">
<input type="hidden" name="stage" value="register">
<br>
<input type="submit" name="submit" value="submit">
</form>
EOREGFORM;

$rate_form = <<< EORATEFORM
<p>My boss is</p>
<form method="post" action="$thisfile">
<input type="radio" name="rating" value=1>
Driving me to look for a new job.<br>
<input type="radio" name="rating" value=2>
Not the worst. But pretty bad.<br>
<input type="radio" name="rating" value=3>
Just so-so.<br>
<input type="radio" name="rating" value=4>
Pretty good.<br>
<input type="radio" name="rating" value=5>
A pleasure to work with.<br><br>
Boss name: <input type="text" size=25 name="boss"><br>
<input type="hidden" name="stage" value="rate"><br><br>
<input type="submit" name="submit" value="submit">
</form>
EORATEFORM;

if (!$_POST['submit'])
{//O1
//First time. Just show registration form
$message = $reg_form;
}//C1
elseif ($_POST['submit'] == 'submit' && $_POST['stage'] == 'register')
{//O2
//Second time. Show the registration for again on error
//Rating form on successful INSERT

if (!$_POST['name'] || $_POST['name'] == "" || strlen($_POST['name']>30) || !$_POST['email'] || $_POST['email'] == "" || strlen($_POST['email']>30))
{//O3
$message = '<p>There is a problem. Did you enter a name and email address?</p>';
$message .= $reg_form;
}//C3
else
{//O4
//Open connection to the database
mysql_connect('xxxx', 'xxxx', 'xxxx')
or die('failure to connect to server');

mysql_select_db('xxxx')
or die('could not connect to database');

//Check to see if name and email have been used before
$as_name = addslashes($_POST['name']);
$tr_name = trim($as_name);
$as_email = addslashes($_POST['email']);
$tr_email = trim($as_email);
$query = "SELECT * FROM raters WHERE name = '$tr_name' AND email = '$tr_email'";

$result = mysql_query($query);
if (mysql_num_rows($result) > 0)
{//05
error_log(mysql_error());
$message = 'Someone with this name and password has already rated. If you think a mistake has been made, please email help@example.com,';
}//C5
else
{//O6
//Insert name and email address
$query = "INSERT INTO raters (id, name, email) VALUES (null, '$tr_name', '$tr_email')";

$result = mysql_query($query);
if (mysql_affected_rows() == 1)
{//07
$message = $rate_form;
}//C7
else
{//08
error_log(mysql_error());
$message = '<p>Something went wrong with your signup attempt.</p>';
$message .= $reg_form;
}//C8
}//C6
}//C4
}//C2
elseif ($_POST['submit'] == 'submit' && $_POST['stage'] == 'rate')
{//O9
//Third time. Store the rating and boss's name

//Open connection to the database
mysql_connect('db1341.oneandone.co.uk', 'dbo235515377', '2X5V6hgS')
or die('failure to connect to database');

mysql_select_db('db235515377');

//Insert rating and boss's name
$as_boss = addslashes($_POST['boss']);
$tr_boss = trim($as_boss);
$rating = $_POST['rating'];
$query = "INSERT INTO ratings (id, rating, boss) VALUES (NULL, '$rating', '$tr_boss')";
$result = mysql_query($query);

if (mysql_affected_rows() == 1)
{//O10
$message = '<p>Your rating has been submitted.</p>';
}//C10
else
{//011
error_log(mysql_error());
$message = '<p>Something went wrong with your rating attempt. Please try again.</p>';
$message .= $rate_form;
}//C11
}//C9
?>	



<html>
<head>
</head>
<body>
<table border="0" cellpadding="10" width="100%"">
<tr>
<td bgcolor="#f0f8ff" align="center" valign="top" width="17%"></td>
<td bgcolor="#ffffff" align="left" valign="top" width="83%"><?php echo $message; ?></td>
</tr>
</table>
</body>
</html>

 

 

Cheers

RichG

Link to comment
Share on other sites

I made the changes you said and the first error kicked in and did not recognize my entry and said did you enter an email even thought i did. The i took the strlen out completly and the error came up again saying registration attempt failed so I think the example in the book must be right with the strlen example and it is something to do with my line i said from //O6.

 

Could it be to do with the DB connection and i need to open and close the connection at each line of DB interactivity or am i ok opening the link and then leaving it open for the whole code?

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.