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>

Comments

Please show how you are reading in the value for gamestart from the HTTP request

Written by AJ

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)

Written by James Allardice

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

Written by Shef

Accepted Answer

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.

Written by bazmegakapa
This page was build to provide you fast access to the question and the direct accepted answer.
The content is written by members of the stackoverflow.com community.
It is licensed under cc-wiki