Jump to content

I've started coding my script


anticore

Recommended Posts

I'm trying to create a system to store user rides in a table to create a simple gallery
I've started coding it and this is what i got.

[code]
$db = mysql_connect ("$host", "$username", "$password");
mysql_select_db ("$databasename", $db);

//Start add article
if($action=="add")
{

//If they have entered data into the form, do this
if($name||$year||$yearcat||$details||$image1||$image2||$image3||$image4||$smpic)
{

$insertIt=mysql_query("INSERT INTO $tablename (`name`, `year`, `yearcat`, `details`, `image1`, `image2`, `image3`, `image4`, `smpic`) VALUES ('$name', '$year', '$yearcat', '$details', '$image1', '$image2', '$image3', '$image4', '$smpic')",$db);
}
if($insertIt)
{
echo " <i>$name</i> has been successfully added<br>";
}
else
{
echo "Sorry there has been an error, please try again<br>";
}
}

//give form
else
{
echo"

<form id=\"form1\" name=\"form1\" method=\"post\" action=\"$PHP_SELF\">
  <label>Name
  <input name=\"name\" type=\"text\" id=\"name\" maxlength=\"30\" />
  </label>
  <p>
    <label>Year
    <input name=\"year\" type=\"text\" id=\"year\" size=\"6\" maxlength=\"4\" />
    </label>
  </p>
  <p>
    <label>Year Catagory
    <select name=\"yearcat\" id=\"yearcat\">
      <option value=\"05up\">05up</option>
      <option value=\"9904\">9904</option>
      <option value=\"9498\">9498</option>
      <option value=\"8793\">8793</option>
      <option value=\"7986\">7986</option>
      <option>Choose One</option>
    </select>
    </label>
  </p>
  <p>
    <label>Details
    <textarea name=\"details\" cols=\"40\" rows=\"6\" id=\"details\" maxlength=\"255\"></textarea>
    </label>
  </p>
  <p>Locations of Images:</p>
  <p>
    <label>Image1
    <input name=\"image1\" type=\"text\" id=\"image1\" />
    </label>
  </p>
  <p>
    <label>Image 2
    <input name=\"image2\" type=\"text\" id=\"image2\" />
    </label>
  </p>
  <p>
    <label>Image 3
    <input name=\"image3\" type=\"text\" id=\"image3\" />
    </label>
  </p>
  <p>
    <label>Image 4
    <input name=\"image4\" type=\"text\" id=\"image4\" />
    </label>
  </p>
  <p>
    <label>Thumbnail Image
    <input name=\"smpic\" type=\"text\" id=\"smpic\" />
    </label>
</p>
  <p><input name=\"action\" type=\"hidden\" value=\"add\">

<input type=\"submit\" value=\"Add Article\"></p>
</form>
";
?>
[/code]

i tried doing userride.php?add to test this and all i got was a blank page
Link to comment
Share on other sites

Try changing:[code]if($action=="add")[/code]to[code]if($_GET['action']=="add")[/code]Only chnage it if you have register_globals set to Off.

If you still dont get anything try going to this instead userride.php?add=1

As in order for the add variable to be sent it needs to have a value assign to it, in this case 1
Link to comment
Share on other sites

Also your if statement with all the variables separated by || is not going to do what you want. If register globals is turned off (I strongly recommend off for security reasons) then you must use something like $_GET['name'] to return the value of the variable and if you are processing it from a form then you want all values to be entered and would need to use && (and) not || (or). "or" will return the statement true and fire the code inside the if after one of the conditions returns true. "and" will return true and fire the code inside the if only if all conditions are met. Grouping them all together like this will keep you from returning an accurate error to the end-user so they don't know what was wrong with what they submitted. Instead, I suggest this:

[code]if ( $_GET['name'] == "" ) die("<p>You didn't enter a name.</p>\r\n");
if ( $_GET['year'] == "" ) die("<p>You didn't enter a year.</p>\r\n");
if ( $_GET['yearcat'] == "" ) die("<p>You didn't enter a yearcat.</p>\r\n");
if ( $_GET['details'] == "" ) die("<p>You didn't enter any details.</p>\r\n");
if ( $_GET['image1'] == "" ) die("<p>You didn't enter image1.</p>\r\n");
if ( $_GET['image2'] == "" ) die("<p>You didn't enter image2.</p>\r\n");
if ( $_GET['image3'] == "" ) die("<p>You didn't enter image3.</p>\r\n");
if ( $_GET['image4'] == "" ) die("<p>You didn't enter image4.</p>\r\n");
if ( $_GET['smpic'] == "" ) die("<p>You didn't enter smpic.</p>\r\n");
[/code]

The draw back of this it will only tell them one error at a time. Alternatively instead of die you can set $error = "" then parse each if statement and where it says die above do $error .= "<p>You didn't do whatever.</p>\r\n"; once all the if statements are done you can do if ( strlen($error) > 0 ) die($error);. That will return all errors and stop the script from going any further.

If you need a more indepth explanation, let me know.
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.