0

I have a php file which doesnt want to work in the browser.

<!DOCTYPE html>
<html lang="en">
    <head>
        <title>Insert A New Skateboard</title>
        <meta charset="utf-8">
        <meta name= "keywords" content = "skateboard">      
<!--[if lt IE 9]>
<script src="http://html5shiv.googlecode.com/svn/trunk/html5.js">
</script>
<![endif]-->
    </head>
    <body>
    <div id="page">
        <form name="variable_key" action="insertskateboard.php" method="post"> <!--bring back the form if there is error in the user entry -->
        <p>Skateboard ID: <input type="text" name="skateboardid" size="15" maxlength="15" /></p>
        <p>Deck: <input type="text" name="deck" size="20" maxlength="40" /> </p>
        <p>Trucks: <input type="text" name="trucks" size="10" maxlength="20" /> </p>
        <p>Wheels: <input type="text" name="wheels" size="10" maxlength="20" /> </p>
        <p>Image: <input type="text" name="image" size="60" maxlength="50" /> </p>
        <p><input type="submit" name="submit" value="submit" /></p>
    </form>

    <?php
      include('function.php');
      $skateboardid = trim($_POST['skateboardid']);
      $deck = trim($_POST['deck']);
      $trucks = trim($_POST['trucks']);
      $wheels = trim($_POST['wheels']);
      $image = trim($_POST['image']);   

      /*$query_age = (isset($_GET['query_age']) ? $_GET['query_age'] : null);*/

      if (!empty($skateboardid) || !empty($deck) ||!empty($trucks) || !empty($wheels) || !empty($image))
      {

        // Create the insert query
        $query = "insert into skateboard values('$skateboardid', '$deck', '$trucks', '$wheels', '$image')";
        // Execute the query and capture the number of rows altered
        $result_set = mysql_query($query, RMITconnect());
        print "<h4>You have successfully added a skateboard to the database </h4>";
        print "<h4>To see the skateboard you just added, click on the <a href= 'products.php'> Products </a></h4> ";        
    } else {
      print "<h4>You have not entered the form information </h4>";
    }

  ?>
    </div>
  </body>
</html>

When trying to open the insertskateboard.php, I receive the error:

Notice: Undefined index: skateboardid in /home/sh5/s3520755/public_html/insertskateboard.php on line 25

Stephan Bauer
  • 9,120
  • 5
  • 36
  • 58

3 Answers3

0

Do like below when you use post method:

    if(isset($_POST))
    {
       $skateboardid = trim($_POST['skateboardid']);
       $deck = trim($_POST['deck']);
       $trucks = trim($_POST['trucks']);
       $wheels = trim($_POST['wheels']);
       $image = trim($_POST['image']); 

    /*$query_age = (isset($_GET['query_age']) ? $_GET['query_age'] : null);*/

    if (!empty($skateboardid) || !empty($deck) ||!empty($trucks) || !empty($wheels) || !empty($image))
    {

        // Create the insert query
        $query = "insert into skateboard values('$skateboardid', '$deck', '$trucks', '$wheels', '$image')";
        // Execute the query and capture the number of rows altered
        $result_set = mysql_query($query, RMITconnect());
        print "<h4>You have successfully added a skateboard to the database </h4>";
        print "<h4>To see the skateboard you just added, click on the <a href= 'products.php'> Products </a></h4> ";        
    }
    else 
    {
        print "<h4>You have not entered the form information </h4>";
    }


}
Virb
  • 1,639
  • 1
  • 16
  • 25
0

You need to check if your indizes are set. If you just call the script and $_POST is empty, those indizes are not available. Try this:

if (isset($_POST['skateboardid']) && isset($_POST['deck']) ...) {
    $skateboardid = trim($_POST['skateboardid']);
    ...
}
rbr94
  • 2,227
  • 3
  • 23
  • 39
0

"When trying to open the insertskateboard.php I receive the error:"

Well. If you mean open it in the browser. then this is normal because the request comes by get method not post

wrap your form handling code inside if($_SERVER['REQUEST_METHOD'] == "POST") like this

if($_SERVER['REQUEST_METHOD'] == "POST"){
   $skateboardid = trim($_POST['skateboardid']);
   $deck = trim($_POST['deck']);
   $trucks = trim($_POST['trucks']);
   $wheels = trim($_POST['wheels']);
   $image = trim($_POST['image']);
}
Accountant م
  • 6,975
  • 3
  • 41
  • 61
  • 2
    _"the form is delivered by get method not post"_ What? his form has `method="POST"` - I think I get what you are trying to say, but you are explaining it very poorly. – Epodax Sep 22 '16 at 10:14
  • @Epodax he said he is getting the warning when he **"opens"** the file, which I guess he means open the file in the browser – Accountant م Sep 22 '16 at 10:17
  • @Epodax I'm sure he does not get the warning when he submit the form – Accountant م Sep 22 '16 at 10:18
  • Yeah, I get that, but the "form" isn't delivered with GET, accessing a page hasn't got anything to do with a GET request unless there are data being sent along with the URL. – Epodax Sep 22 '16 at 10:20
  • @Epodax yes you are right .thank you . yes I explained it wrong. edited it – Accountant م Sep 22 '16 at 10:22