URL Variables

Hi Guys,

So i’ve almost sorted my “read more problem”. Below is the code i’m using to generate each page once the read more button has been clicked on the individual story on the home page.

[php]

<?php $storyid = $_GET["storyid"]; // Assuming you've already connected to your database: $result = mysql_query("SELECT * FROM banter WHERE P_Id = '$storyid'"); // Replace "table_name" with the name of your table. // And replace "row_primary" with the name of the column you marked as the primary key. while ($row = mysql_fetch_array($result)) { echo "
"; echo "
{$row['P_Id']}
"; // Replace "column_one" with the name of your first column. echo "
{$row['story']}
"; // Replace "column_two" with the name of your second column. echo "
{$row['screen_name']}
"; // Replace "column_three" with the name of your third column. echo "
"; } mysql_close($con); ?>

[/php]

When i write a url manually i.e. www.banterdonkey.com/readmore.php?storyid=1 it loads perfectly. The only trouble I’m having now is to get each read more button on the home page to post the storyid to the url.

This is the code i’m using for the home page.

[php]<?php
$con = mysql_connect(“host”,“user”,“pass”);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

mysql_select_db(“database”) or die(mysql_error());

// Assuming you’ve already connected to your database:
$result = mysql_query(“SELECT * FROM banter ORDER BY P_Id ASC”);
// Replace “table_name” with the name of your table.
// And replace “row_primary” with the name of the column you marked as the primary key.

while ($row = mysql_fetch_array($result)) {
echo “

”;
echo “
{$row[‘P_Id’]}
”; // Replace “column_one” with the name of your first column.
echo “
{$row[‘story’]}
”; // Replace “column_two” with the name of your second column.
echo “
{$row[‘screen_name’]}
”; // Replace “column_three” with the name of your third column.
echo “
<form action=‘readmore.php?storyid=[‘P_Id’]’>
”; // Your submit button. (adds one to each row)
echo “
”;
}

mysql_close($con);
?>[/php]

All i’m trying to do is get the P_Id unique to of each story to appear in the url where the storyid=…

Can anyone suggest a solution as my way is not working!

Thanks

Sam

Hi there,

Try this:
[php]<form action=‘readmore.php?storyid=’.$row[‘P_Id’].’>[/php]

You needed $row[‘P_Id’] instead of just [‘P_Id’]. Hope this helps.

Thank you for the suggestion. I get a syntax error.

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in D:\Hosting\8263933\html\bdwebsite\index.php on line 60

I changed this

to this

which fixes the syntax error but now the button doesn’t appear on the page. I know we must be close but my limited knowledge of php is making me struggle to fix this.

Sam

Hi again,

I’m sorry for giving you the wrong information. Use this code instead and tell me if this works:
[php]echo “

”[/php]

Note I removed the ‘. and .’ as well as the quotes surrounding the array key name.

Let me know if this helps you.

Hiya,

Thanks once again. This just loads www.banterdonkey.com/readmore.php? it does not include the variable. I’ve only been learning php for 2weeks so it might be more clear if you check out the site - its only a basic structure at the moment.

Many thanks,

Sam

Hi again,

Repost your updated code so I can look at it. You might have an error somewhere.

Ok no problem

home Page:

[php]<?php
$con = mysql_connect(“host”,“user”,“password”);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

mysql_select_db(“database”) or die(mysql_error());

// Assuming you’ve already connected to your database:
$result = mysql_query(“SELECT * FROM banter ORDER BY P_Id ASC”);
// Replace “table_name” with the name of your table.
// And replace “row_primary” with the name of the column you marked as the primary key.

while ($row = mysql_fetch_array($result)) {
echo “

”;
echo “
{$row[‘P_Id’]}
”; // Replace “column_one” with the name of your first column.
echo “
{$row[‘story’]}
”; // Replace “column_two” with the name of your second column.
echo “
{$row[‘screen_name’]}
”; // Replace “column_three” with the name of your third column.
echo “
”; // Your submit button. (adds one to each row)
echo “”;
}

mysql_close($con);
?>[/php]

Readmore.php

[php]<?php
$con = mysql_connect(“host”,“user”,“password”);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

mysql_select_db(“database”) or die(mysql_error());

$storyid = $_GET[“storyid”];

// Assuming you’ve already connected to your database:
$result = mysql_query(“SELECT * FROM banter WHERE P_Id = ‘$storyid’”);
// Replace “table_name” with the name of your table.
// And replace “row_primary” with the name of the column you marked as the primary key.

while ($row = mysql_fetch_array($result)) {
echo “

”;
echo “
{$row[‘P_Id’]}
”; // Replace “column_one” with the name of your first column.
echo “
{$row[‘story’]}
”; // Replace “column_two” with the name of your second column.
echo “
{$row[‘screen_name’]}
”; // Replace “column_three” with the name of your third column.
echo “
”;
}

mysql_close($con);
?>[/php]

Many thanks,

Sam

Hi again,

For what you are wanting to do, you can simply use links instead of form buttons; it’s saves lines of code and it’s not only easier to you the developer, but for your code as well. Instead of adding the form tags to your code, add the following code to your script and check to see if that works and if you may want to use this technique:
[php]echo “

Read more
”;[/php]

Also, when I took a look at your page source, your form action had storyid=(1) instead of storyid=1. You might want to find out why the code is adding the ( )'s to the url. You may want to check your database and see if it’s in there.

I hope this helps.

Hi OpzMAster,

That worked perfectly. The reason for the () on the page source code was me experimenting trying to see if i could get the button to work properly. Sorry for the confusion!

I am more than happy to use the hyperlink method, but out of interest and for my future knowledge can you think of any reason as to why the previous code wouldn’t have worked?

Many thanks,

Sam

I think it’s more of an HTML or browser issue than anything else. I will, however, show you how to use the FORM submit technique in case you want to use it in the future. The only additional thing you need is a hidden field.

[php]echo “”;[/php]

If you look at your old code in the browser’s source, it does point to where you want it to go, but I guess while processing the script, it (PHP/browser) doesn’t like it and just transmit the page. However, I’ve provided you with a guaranteed way of using the FORM submit.

Okay thank you.

2 more quick questions for you if you have the time to explain this to me. I;ve been posting for a few weeks now about a problem i’ve been having with encryption.

Basically on my registration form I wanted to submit the users password using md5 or sha1. When they go to post a story I would then encrypted their entry and compare against the already encrypted entry in the database. However when i use encryption I always get the error saying the passwords do not match. At the moment i’ve taken to storing the passwords without encryption - which is not ideal to say the least! - but everything is working as it should. The code for both the registration page and the submit page can be seen here http://www.phphelp.com/forum/index.php/topic,13533.0.html

secondly, when the user submits a story after matching their records with the “login” table in the database it posts the “story” and “screen_name” to a table called “banter”. When a story is submitted as well as inserting it into the “banter” table i would like it to update their last login date and time in the “login” table. This is not a problem when I do the insert and the update separately - both are just simple sql statements. However when I try and combine them I can only get one to run - if i update the time the post doesnt appear in the “banter” table and vice versa.

Basically what im trying to say is…

why does everything work perfectly until I try and add encryption

and

is there a simple way in php to say INSERT this in to database 1 and after that UPDATE this in database 2

Many thanks,

Sam

As far as the encryption goes, I use sha1 in my scripts and encrypt the password on registration and put it in the database as the sha1 hash string and when the user logs in, I use the following technique:

[php]$user = $_POST[‘username’];
$pass = sha1($_POST[‘password’]);

$result = $db->query(“SELECT * FROM

WHERE user=’$user’ AND pass=’$pass’”);[/php]

I use mysqli for my scripts so that’s why it looks different but you can get the hint. If the query returns anything, that means that the username and password matches the one in the database than the credentials worked.


Now as for your second problem. I would just leave it at two separate queries. It helps with organization and will help you make sense of your code in the future if you don’t edit your code for a while.

Thank you for your help. I just want to clarify this quickly…

as far as your encryption scripts go I believe they are almost identical to mine and I don’t understand why mine doesn’t work?

Are you suggesting I have a separate table for last login time and date with 3 columns i.e. (screen_name, date, time)?

Many thanks

Sam

You would have to post your encryption script on these forums for me to assist you further with that. As far as the database table thing, all I’m saying is that you should have one table for users that contains the username, password, contact, last_login, etc and keep it separate from other aspect of your site like news and such. It’s just better to have separate queries for INSERT, UPDATE, SELECT, and DELETE as in the future when you or others look at your script, it just makes more sense and it’s more organized. Believe me, that extra work will be most beneficial in the future.

Heya,

Sorry i think i may have confused the situation. Thats exactly what i have - two separate databases. What I’m asking is can I insert data into one table and update information in another using the same form?

Thanks,

Sam

Hi again,

If I understand you correctly, yes you can. You will just have one query after the other. It can be either and you can say that if the first query ran successfully, run the other. It’s quite easy and straightfoward.

Hope that helps.

Thank you for that. I thought you could but I just wanted to make sure.

Can you have a variable within a form action? On my readmore.php I have added a “comment” box. I would like it so that when people commented it would link it to that particular story. I read that a simple way to do this was to give the comments a parent id that related to each story number. That way each time a story was called up the related comments would also be called up with it.

What I have done is created a table called comments with 4 columns (C_Id, parent, comment, screen_name).The C_Id is the primary key and is auto increment. When a user enters a comment on a particular story I would like the parent to be the same as the P_Id on the readmore page. I thought the easiest way to do this would be to copy the line of code you have already helped me with for the index page Read more and add it to my form action=

This is the read more code i came up with.

[php]

BanterDonkey | Read More <?php $con = mysql_connect("host","user","password"); if (!$con) { die('Could not connect: ' . mysql_error()); }

mysql_select_db(“database”) or die(mysql_error());

$storyid = $_GET[“storyid”]; // gets storyid from the url

// Assuming you’ve already connected to your database:
$result = mysql_query(“SELECT * FROM banter WHERE P_Id = ‘$storyid’”);
// Replace “table_name” with the name of your table.
// And replace “row_primary” with the name of the column you marked as the primary key.

while ($row = mysql_fetch_array($result)) {
echo “

”;
echo “
{$row[‘P_Id’]}
”; // Replace “column_one” with the name of your first column.
echo “
{$row[‘story’]}
”; // Replace “column_two” with the name of your second column.
echo “
{$row[‘screen_name’]}
”; // Replace “column_three” with the name of your third column.
echo “
”;
}

mysql_close($con);
?>

// up to here works fine. My problem is the next line



Please enter your screen name. If you currently do not have a screen name you can register for one here

Please enter your password. Forgotten your password? Click here

[/php]

However when i click submit it doesn’t grab the P_Id it just appears exactly as this www.banterdonkey.com/comment.php?storyid=$row[P_Id] - its not adding the variable.

Is there a better way of carrying that P_Id forward? For completeness here is the code for comment.php

[php]<?php
//This makes sure they did not leave any fields blank

if (!$_POST[‘comment’] || !$_POST[‘screen_name’] ) {

	die('You did not complete all of the required fields');

}

$storyid = $_GET[“storyid”];

// Connection to database
$con = mysql_connect(“host”,“user”,“pass”);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

mysql_select_db(“database”) or die(mysql_error());

// Limit the result to only one row, because there should only be one user with that screen_name:
$result = mysql_query(“SELECT * FROM login WHERE screen_name=’$_POST[screen_name]donkey’ LIMIT 0,1”);

// Check to see if it actually got something, if so, continue on:
if(mysql_num_rows($result) != 0) {

// Fetch the row and drop it into an array:
$row = mysql_fetch_array($result);

// Assuming that the password is entered in the database already, :

if ($_POST[‘pass’] == $row[‘pass’]) {

// Password is correct
$sql=“INSERT INTO comments (parent, comment, screen_name)
VALUES
(’$_POST[$storyid]’, ‘$_POST[comment]’,’$_POST[screen_name]’)”;

if (!mysql_query($sql,$con))
{
die('Error: ’ . mysql_error());
}
echo “Thank You for posting”;

mysql_close($con);
}

else {
// Password is incorrect
echo “Your password does not match our records”;
}
}
else {
echo “Your username does not exist”;
}
?>[/php]

Many thanks,

Sam

Hi again,

The problem here is that you are attempting to use PHP without encapsulating it in the PHP tags. Do the following and it should work, I tested it:

[php][/php]

Just fix this line and it should work. Just remember, this line of code is OUTSIDE of the PHP tags and just the small snippet of PHP is added. Also, this should take care of the original question of this post as doing it this way outside of PHP and just including a snippet of PHP in the middle of the HTML seems to include the variable in the URL. Hope this helps.

It seems so obvious when you point it out - its a little bit embarrassing how long have you been doing this? I get an error message “no input file specified” after i click submit.

Many thanks

Sam

Sponsor our Newsletter | Privacy Policy | Terms of Service