Jump to content

[SOLVED] Stuck a little.


Dane

Recommended Posts

Hey guys,

 

I'm not really sure how to write this bit of code so im going to explain the best i can.

 

Basically i want to grab mail_id from mail.php?mail_id=1

if(!empty($_GET['mail_id'])){

  $mail_id = $_GET['mail_id'];
  if(mysql_num_rows($sql)==1){
   $col = mysql_fetch_array($sql);

}

 

Say for instance mail_id=1 is inside the database with the mail information correct.

 

 

But value mail_id=2 is not inside the database.

 

When i goto mail.php?mail_id=2 in my browser i want it to come up with an error saying "no mail" or something like that.

 

How would be the best way to go about this bit of coding?

 

Thanks

 

Dane

Link to comment
Share on other sites

I love simple questions like this :-D Here ya go.

 

<?php
if(isset($_GET['mail_id'])){//if the mail_id is in the url, let's check it

if(!empty($_GET['mail_id']) && is_numeric($_GET['mail_id'])){//it's not empty and it's a number
$mail_id = $_GET['mail_id'];
$query = "SELECT column1,column2,column3,etc FROM table_name WHERE `mail_id` = '$mail_id' LIMIT 1";//set-up query with a limit of 1 to return only 1 row maximum

if(!mysql_num_rows(mysql_query($query))){//1 = true, 0 = false
die("I'm sorry, the mail id that was entered is incorrect.");
}

//if we got to this point, there was data returned
$row = mysql_fetch_array(mysql_query($query));
$column1 = $row['column1'];
$column2 = $row['column2'];
$column3 = $row['column3'];
$etc = $row['etc'];

} else {//$_GET['mail_id'] was either: empty/null or not a number
die("I'm sorry, the mail_id inputted was an incorrect format or empty.");
}
} else {//there's no mail_id in the url

//output the form

}
?>

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.