I have a simple code (very simple one) that I was using to try something out for a work and I was trying a function to work with the variables of a form radio in post method, to update my SQL table with the output of the form. But when I'm going to try it, it doesn't update and gives me a notice.
It has something to do with the query (because the error says is in that line of the code) but I still don't know what it is.
I tried to change the syntax of the SQL sentence in different ways. I changed the user I was going to use to change the "image_value" column. I even checked the syntax of the query in phpmyadmin, and it worked.
Here is the php code:
<?php
mysql_connect("localhost","root","");
function user_image($value){
print_r($value);
//This is the problem
$query = "UPDATE users SET image_value = '$value' WHERE (ID) = '6'";
mysql_query($query);
}
?>
And here i have the code of the form and how I'm using the function (if there is any mistake that I haven't seen)
<form method="post" action="">
<input type="radio" name="1" value="1">imagen1
<br>
<input type="radio" name="2" value="2">imagen2
<br>
<input type="radio" name="3" value="3">imagen3
<br>
<input type="radio" name="4" value="4">imagen4
<br>
<button type="submit"><span>Submit</span></button>
</form>
<?php
user_image($_POST);
?>
Your problem is you are passing the complete object $_POST.
You don't specify if your radio name is correct (The name of your radio as a number from 1 to 4).
In the case, you are trying to set the value of image_value from a radio button should be.
<form method="post" action="">
<input type="radio" name="image_value" value="1">imagen1
<br>
<input type="radio" name="image_value" value="2">imagen2
<br>
<input type="radio" name="image_value" value="3">imagen3
<br>
<input type="radio" name="image_value" value="4">imagen4
<br>
<button type="submit"><span>Submit</span></button>
</form>
<?php
if (isset($_POST['image_value'])) {
user_image($_POST['image_value']);
}
?>
and your function
function user_image($value) {
mysql_connect("localhost","root","");
print_r($value);
//This is the problem
$query = "UPDATE users SET image_value = '$value' WHERE (ID) = '6'"; //ID should be dynamic base on the user I guess
mysql_query($query);
}
?>
$_POST pass an object value to the server, you need to specify the property you want to use, var_dump $value to understand all what it contains.
Related
Having issues with using a form's value in a different php file:
my firstpage.php
<form method="post">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
</form>
my secondpage.php is here
<?php
include("firstpage.php");
$result = $_POST['rdbbtn'];
if ($result == "1") {
echo 'thirdpage.php';
}
else {
echo 'fourthpage.php';
}
?>
problem:
Notice: Undefined index: rdbbtn in
how come I can't use "rdbbtn"? Should I have something like
$rdbbtn = $_POST['rdbbtn'];
in secondpage.php? Tried this but didn't solve my problem.
firstpage.php and secondpage.php are in the same directory.
Probably it's some pretty obvious thing that I don't see...thanks!
EDIT: I have accepted pradeep's answer as that helped me the most to figure what the problem should be. would like to say thank you for everybody else showing up here and trying to help!
When you change current page it reset the value and $_POST is empty.
You can try with set form action to next page . It will work
<form method="post" action="secondpage.php">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
<input type="submit" name="" value="Next">
</form>
Other wise you can make a function in a class and set each page action
to this function.
And set your each form data to session.
Finally when you change the page you read data form session.
Class FormAction{
public function setFormDataToSession(){
if(isset($_POST['rdbbtn']){
$_SESSION['rdbbtn'] = $_POST['rdbbtn'];
}
}
}
In your page simply get the session value.
echo $_SESSION['rdbbtn'];
Should be like this :
Check with isset method in
<?php
include("firstpage.php");
$result = isset($_POST['rdbbtn']) ? $_POST['rdbbtn'] : NULL;
if ($result == 1) {
echo 'thirdpage.php';
}
else {
echo 'fourthpage.php';
}
?>
and your form should be like this :
<form method="post">
<input type="radio" name="rdbbtn" value="1"> One
<input type="radio" name="rdbbtn" value="2"> Two
<input type="submit" name="submit" value="submit">
</form>
Sorry for not being able to comment in this post(less reputations). But seems like you are asking about storing the variables of the session. This way you can use the variables for a whole session. Just start the session by putting session_start() in the very beginning of secondpage.php file and then you can access the variables at any time during the session by simply calling $_SESSION['rdbutton] in any page like fourthpage.php or anything. Just make sure u put the session_start() at the top of each page where you want to use the variables. Don't forget the semicolons at the end. 😜 Hope this helps.
I am a newible on php and I am following a video to learn inserting data in website to database, but I face a huge problem.
As I am going to from many groups from many members, i create some checkboxs for member to register.
I followed this video tutorial,but have error.
<?php
//$_SESSION['name'];
session_start();
echo "student";
?>
<form action="" method="POST">
<input type="checkbox" name="group[]" value="group1">group1<br>
<input type="checkbox" name="group[]" value="group2">group2<br>
<input type="checkbox" name="group[]" value="group3">group3<br>
<input type="checkbox" name="group[]" value="group4">group4<br>
<input type="checkbox" name="group[]" value="group5">group5<br>
<input type="submit" name="submit" value="submit">
</form>
<?php
if (isset($_POST['submit']))
{
//print_r($_POST);
echo implode(',', $POST['group']);
}
?>
</h1>
Logout
All things that I wanna do was followed for these coding but still have error
Warning: implode(): Invalid arguments passed in
C:\xampp\htdocs\fyp\student.php on line 37
echo implode(',', $POST['group']);
what was wrong with it, thank you for any suggestion
You are missing $_ before POST .
Instead of this
echo implode(',', $POST['group']);
use this
echo implode(',', $_POST['group']);
You're forgetting the underscore before the POST. Here is that line fixed:
echo implode(',', $_POST['group']);
There are a few other simple issues that might cause some trouble later on. First, in terms of syntax, you should close your HTML input tags (like below):
<input type="checkbox" name="group[]" value="group1" />group1<br>
...
Next, you will get this same error when no checkboxes are selected as the variable $_POST['group'] has not been defined yet. You can use the PHP empty() function to test this variable (http://php.net/manual/en/function.empty.php) before you call the implode().
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
I have searched the whole internet saw unlimited stack posts followed by letter some tutorials and still i cant get it working but why? it keeps telling me undefined index when i use isset becomes undefined variable. Sorry my coding level is low...
Thanks in advance.
<form name="ghostform" action="insert.php" method="post">
<input type="hidden" name="id" value="1" id="id" />
<input type="hidden" name="eidos" value="2" id="eidos" />
<input type="hidden" name="seidos" value="3" id="seidos" />
<input type="hidden" name="idiotites" value="4" id="idiotites" />
</form>
<?php
mysql_connect("localhost","root","")or die("cant connect to server");
mysql_set_charset('utf8');
#mysql_select_db("cafemanager")or die( "Unable to select database");
$id = $_POST['id'];
$eidos = $_POST['eidos'];
$seidos = $_POST['seidos'];
$idiotites = $_POST['idiotites'];
mysql_query("INSERT INTO Current(id, eidos, specificeidos, idiothtes) VALUES('$id','$eidos','$seidos','$idiotites')");
?>
Use something like this:
<?php
if($_POST) {
mysql_connect("localhost","root","")or die("cant connect to server");
mysql_set_charset('utf8');
mysql_select_db("cafemanager")or die( "Unable to select database");
$id = mysql_real_escape_string(strip_tags($_POST['id']));
$eidos = mysql_real_escape_string(strip_tags($_POST['eidos']));
$seidos =mysql_real_escape_string(strip_tags( $_POST['seidos']));
$idiotites = mysql_real_escape_string(strip_tags($_POST['idiotites']));
mysql_query("INSERT INTO Current(id, eidos, specificeidos, idiothtes) VALUES('$id','$eidos','$seidos','$idiotites')");
}
?>
<form action="" method="post">
<input type="hidden" name="id" value="1" id="id" />
<input type="hidden" name="eidos" value="2" id="eidos" />
<input type="hidden" name="seidos" value="3" id="seidos" />
<input type="hidden" name="idiotites" value="4" id="idiotites" />
</form>
But please start using mysqli not depracted mysql
Every time you need a GET or a POST, I'd recommend you to especify default values in case that you don't get all values, so the function will not throw any warning, or extrange things inside the SQL sentence.
Example:
In this case i define 0 as default.
$id = isset($_POST["id"])?$_POST["id"]:0;
And also, if you are waiting for a number as a value.
$id = isset($_POST["id"])?intval($_POST["id"]):0;
If 0 is an impossible value and don't makes any sense, then you know that something has gone wrong, and you can react to it.
how do you submit this?
and please use if(isset($_POST['name']){ // do stuffs }
The reason it shows "undefined index or variable" is because PHP cannot find the variables in the $_POST array.
If this page is handling inputs from a form, make sure that the action of the form points to this page and the method of the form is set as POST.
<form action="urlofthispage" method="POST">
If this page is also used for handling other scenarios, you can simply check if variables are set in $_POST. The isset function simply checks if the variable has been set. Like this:
if(isset($_POST['id']))
{
//then do what do u want if the variable is present
}
else
{
//then do what do u want if the variable is not present
}
For the official documentation of the isset function, you can check this for reference:
http://in2.php.net/isset
Your actual problem
You assume that PHP can get the values from your fields, which you can't..
Because you need to submit the values somehow to the server before PHP realizes that they are there, either do this by a submit button or send your values via javascript.
PHP
A server side language that is not executed until you request the content from the server. Meaning that if you want these values (that are located in the clients browser) you need to send them (POST) to the server for processing everything between <?php and ?>
Try this code
<form name="ghostform" action="insert.php" method="post">
<input type="hidden" name="id" value="1" id="id" />
<input type="hidden" name="eidos" value="2" id="eidos" />
<input type="hidden" name="seidos" value="3" id="seidos" />
<input type="hidden" name="idiotites" value="4" id="idiotites" />
<input type="submit" value="kablamo">
</form>
Tested the code, and it works.
As long as you have a submit button...
On a side note, here's how isset works
function getVal($var) {
if(!isset($_POST[$var]))
return 'default';
return $_POST[$var];
}
$id = getVal('id');
$eidos = getVal('eidos');
$seidos = getVal('seidos');
$idiotites = getVal('idiotites');
Use isset to catch if the value is set or not.
And you could use a function with default values (this is just a primal example) if the value is not set. Or you could raise some form of exception before connecting to the database.
#MiQUEL's solution is neater, only reason why I'd go with a function is because it's more readable and you can perform operations before setting the value.. like this:
function getVal($var) {
if(!isset($_POST[$var])) {
mysql_connect("localhost","root","")or die("cant connect to server");
mysql_select_db("mydb");
$result = mysql_fetch_array(mysql_query("SELECT defaultValue FROM mytable WHERE name=" . $var), MYSQL_NUM);
return $result[0];
}
return $_POST[$var];
}
Does anyone know why this code will not work? I simply want to print which radio button is selected. It always prints 'Null' no matter what is selected. PHP code is below.
<?php
$conn = mysql_connect('localhost','student','student') or die(mysql_error());
mysql_select_db('vgs',$conn);
//Get Question 1
if (isset($_GET["q1option"]))
{
$q1option = $_GET["q1option"];
}
else
{
$q1option = "Null";
}
//Process Question 1
echo "".$q1option;
The HTML code is below.
<form action="" method="get" >
<div id="Q1">
<label><input type="radio" name="q1option" value="Less_than_16" />Less than 16</label><br />
<label><input type="radio" name="q1option" value="16_or_more" />16 or more</label>
</div>
Any help with this would be greatly appreciated. Note I have many tables in the 'vgs' database, if that makes a difference.
Thank you,
Daniel
Additional Code
HTML below
<input type="button" value="Submit" onclick="result();" />
<input type="reset" value="Reset" />
</form>
Embedded JavaScript below. It uses http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js
function result()
{
$('#Suggestion').load('process_answers.php');
}
Looks like your submit button is outside of the <form> tag, it should be within the form:
<form action="" method="get" >
<div id="Q1">
<label><input type="radio" name="q1option" value="Less_than_16" />Less than 16</label><br />
<label><input type="radio" name="q1option" value="16_or_more" />16 or more</label>
</div>
<input type="submit" value="Submit" />
</form>
You are always getting Null in return because your query string is not being attached anywhere. If you are using AJAX, you need to pass the query string data. Your cycle is not working properly because of a misconfiguration.
Take the following code:
//Get Question 1
if (isset($_GET["q1option"]))
{
$q1option = $_GET["q1option"];
}
else
{
$q1option = "Null";
}
Using $_GET will attempt to extract from your query string a variable labeled 'q1option'. If this variable is not set, then return 'Null'. So your code is working as expected as it is currently structured.
If you want to pass the variable between pages you will either need to submit the form and the variable will be automatically passed, or you need to modify your jQuery to allow for the passing and processing behind the scenes of your variable. Once processed, you would then handle the results accordingly and render them to the client as desired.
I wrote this statements but it is not work :(
... can you tell me why?
HTML:
<form action="join.php" method="post">
<label name="RoomName">Room1</label>
</form>
PHP:
$roomName = $_POST['RoomName'];
$roomID = "SELECT RoomID FROM rooms WHERE RoomName = $roomName";
EDIT:
thanks but in my work the user does not have the ability to edit the room name
so i need to display the room name in a label (on any thing else) instead of text box
You need an <input> element as well.
<input type="text" name="RoomName">
This way the value is available by $_POST['RoomName']. You likely also need a submit button:
<input type="submit" value="Submit">
The label just associates the label with an input element, usually with the for attribute pointing to the input element's id:
<label for="RoomName">Room1</label>
<input type="text" id="RoomName" name="RoomName">
The benefit of this is mainly in accessibility (screen readers, clicking label, etc).
To learn more about HTML forms, go through this quick guide: http://www.w3schools.com/html/html_forms.asp
As to the SQL query, read the comments others posted to your question. You need to quote strings and escape the values from SQL injections as well.
Update: as per your edit, just set the readonly attribute to avoid the field being edited:
<input type="text" id="RoomName" name="RoomName" value="somevalue" readonly>
or make use of a hidden input element:
<input type="hidden" name="RoomName" value="somevalue">
Your code should look like this instead:
<form action="join.php" method="post">
<label name="RoomName">Room Name:</label>
<input type="text" name="RoomName" value="Room 1" />
<input type="submit" value="Submit Room" />
</form>
Also, you can't just set the value to the SQL query. You need to use the mysql_fetch_assoc() function. So it would be more like:
$sqlQuery = "SELECT RoomID FROM rooms WHERE RoomName = '".mysql_real_escape_string($roomName)."'";
$result = mysql_query($sqlQuery);
while ($row == mysql_fetch_assoc($result)) {
$roomID = $row['rooms'];
//do stuff with the current roomID
}
RoomName = $roomName"
to
RoomName = '$roomName'"
In SQL, strings must be quoted. Also, be safe by doing mysql_real_escape_string() on $roomName.