Reputation: 207
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>
Upvotes: 2
Views: 3743
Reputation: 227240
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']
.
Upvotes: 0
Reputation: 78681
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.
Upvotes: 7
Reputation: 165971
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')";
Upvotes: 2