2

I have several select statements that look like this. I need to pass the php variable into my database.

 <select name="gamestart" >
 <?php for($gamestart=1; $gamestart<=24; $gamestart++)
 echo "<option value='$gamestart'>$gamestart:00</option>"; 
 ?>
 </select>

Here is my insert statement into my database with other similar variables where I have the same issue.

     <?php 
   $mysql_query = "INSERT INTO soccer_games (gamestart, gameend)
        VALUES ('$gamestart', '$gameend')";

    $this->db->query($mysql_query);
   ?>

As of now, values are being put into the database but the value that is INSERT'ed into the database is the number 25 (not even an option in my loop as you can see) regardless of what value I select in the form.

===============

EDIT 1: The problem would probably be that I haven't used a form (Me in my infinite wisdom didn't realize I needed a form for this).

I am assuming I will need something of this nature...

public function insertstatements(){
<?php 
   $mysql_query = "INSERT INTO soccer_games (gamestart, gameend)
        VALUES ('$gamestart', '$gameend')";

    $this->db->query($mysql_query);
   ?>
}


<form method="post" action="<?php insertstatements(); ?>">


 <select name="gamestart" >
 <?php for($gamestart=1; $gamestart<=24; $gamestart++)
 echo "<option value='$gamestart'>$gamestart:00</option>"; 
 ?>
 </select>
</form>
3
  • 1
    Please show how you are reading in the value for gamestart from the HTTP request Commented Jun 15, 2011 at 17:22
  • 2
    Where do you assign a value to $gamestart and $gameend before the query? I have a feeling you're running the insert code before the form has been submitted... (that's why gamestart is 25, the value it would have once your loop has finished) Commented Jun 15, 2011 at 17:23
  • Show us how you are getting the value from GET/POST for $gamestart on your query, and all the operations you are doing with it. Upd: Yay three same questions hitting you Pascal, try to chew that... :P Commented Jun 15, 2011 at 17:23

3 Answers 3

7

You can access the gamestart sent from the form using either $_POST['gamestart'] or $_GET['gamestart'] depending on the method set in your form (if there is no method attribute, it will be GET).

Your $gamestart is set to 25 before I guess the for loop is before your database insertion logic, so its value will be set to 25.

Be sure to use mysql_real_escape_string on your variable before putting it into the query, otherwise you might suffer some SQL injection attacks, quite easily. Never trust user input!

So a correct example would be (I don't know where you're getting $gameend from, but if it's also from the form, do the same with it):

$gamestart=mysql_real_escape_string($_POST['gamestart']);
$mysql_query = "INSERT INTO soccer_games (gamestart, gameend)
        VALUES ('$gamestart', '$gameend')";

EDIT: You can access your GET and POST variables simply as $gamestart if you have register_globals enabled. But you should turn it off, and not write any code that depends on it. For the whys, search for it on Google.

Sign up to request clarification or add additional context in comments.

Comments

2

Your form tag should look something like this: <form action="thepage.php" method="post">

You need to put the URL of a file in the action attribute (you can use the same page that the form is on if you like). When the form is submitted, the data is sent to that page via HTTP POST (in this case, it could be GET if you specified method="get").

On the page you send the data to, you can retrieve the data from your form by using $_POST["fieldName"].

You could therefore do something like this (assuming gameend is the name of another field in your form):

$gamestart = mysql_real_escape_string($_POST["gamestart"]);
$gameend = mysql_real_escape_string($_POST["gameend"]);
$mysql_query = "INSERT INTO soccer_games(gamestart, gameend) VALUES('$gamestart', '$gameend')";

Comments

0

Your form tag doesn't work the way you want.

<form method="post" action="<?php insertstatements(); ?>">

What this will do is run insertstatements and then echo nothing, thus making a form tag like:

<form method="post" action="">

A blank action will make the script POST to itself. If this is what you want then leave it, or you can make it post to a certain page, by setting action to that page.

<form method="post" action="submit.php">

To read the variables from the form use, $_POST, eg: $_POST['gamestart'].

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.