I'm having some problems trying to post $_GET variables into a table.
Here is my script:
include 'connect.php';
if(isset($_POST['client_name'])) {
$_GET['list']; //these are variables passed through from another page and I want these to post in the same table this page is suppose to post in.
$_GET['list_id'];
$Cname = $_POST['client_name'];
$Cnumber = $_POST['client_number'];
$listid = $_POST['list_id'];
$listname = $_POST['list'];
if(!empty($Cname) && !empty($Cnumber)){
$query = "INSERT INTO clients (id, client_name, client_number, list_name, date_registered, list_id) VALUES ('$userid','$Cname', '$Cnumber', '$listname', now(), '$listid')";
mysql_query($query);
echo '<br />
<br />
You successfully added a new clients to your list View Update';
mysql_close();
}
else {
echo '<script type="text/javascript">alert("Both fields are required");</script>';
}
Whenever I run the script everything else but the listname and list_id is posted in the database table.
I tried assigning the get variables to new variable such as
$listNAME = $_GET['id'];
but even with that I still end up with empty fields in my table
I even tried to use the $_GET variable in the mysql INSERT query and still no luck
Can anyone help me out and give me some advice as to what I can do to solve the empty fields when the script runs.
<form action="addclient.php" method="POST">
Name of Client: <input type="text" name="client_name">
Client's Number: <input type="text" name="client_number" placeholder="1-876-xxx-xxx">
<input type="submit" >
</form>
You say you have $_GET variables, but you are trying to retrieve them as $_POST variables:
$listid = $_POST['list_id'];
$listname = $_POST['list'];
Isn't it the issue? You could also try this to see what's comming in both arrays:
print_r($_GET);
print_r($_POST);
Alternatively, you could use $_REQUEST as it receives either $_GET or $_POST variables.
I say it only to notice .
Please use PDO or mysqli
if you are calling your addclient.php like
http://localhost/addclient.php?list_id=100&list=mylistname
than you must catch both variables in addclient.php
if (isset($_GET['list_id'])) {
$listid = $_GET['list_id'];
$listname = $_GET['list'];
}
and your form
<form action="addclient.php" method="POST">
<input type="hidden" name="list_id" value="$listid">
<input type="hidden" name="list" value="$listname">
Name of Client: <input type="text" name="client_name">
Client's Number: <input type="text" name="client_number" placeholder="1-876-xxx-xxx">
<input type="submit" >
</form>
and after submit
if(isset($_POST['client_name'])) {
$Cname = $_POST['client_name'];
$Cnumber = $_POST['client_number'];
$listid = $_POST['list_id'];
$listname = $_POST['list'];
....
}
and in your insert
VALUES ('$userid','$Cname', '$Cnumber', '$listname', now(), '$listid')
$listid without quotes it's a int(11) .
VALUES ('$userid','$Cname', '$Cnumber', '$listname', now(), $listid)
Related
I wanted to store list of users from php form in a php session
I defined an empty array at the beginning of the session and tried to
collect the user names at every submit.
session_start();
$persons=array();
if (isset($_POST)) {
$_SESSION['val']=$_POST['val'];
$persons=$_SESSION['val'];
}
foreach($persons as $d){
echo $d;
echo '</br>';
}
<form action="exam.php" method="post">
Enter a new Person: <input type="text" name = "val">
<input type="submit" name = "send">
</form>
I expected to have the list of persons I submitted returned from the
array
but every time I submit, the last submit replaces the first one.
You are overwriting the array each time:
$persons = $_SESSION['val'];
In order to push data to an array in php you must do it this way:
$persons[] = $_SESSION['val'];
If what you want to is store all persons on session, without overwriting them each time you first need to check if session exist, if not, create it.
if(!isset($_SESSION['persons'])){
$_SESSION['persons'] = array()
}
And then change how you store the info in the session, example:
$_SESSION['persons'][] = $_POST['val'];
Then you can do:
foreach($_SESSION['persons'] as $d){
echo $d;
echo '</br>';
}
So the code will look like:
session_start();
$persons=array();
if(!isset($_SESSION['persons'])){
$_SESSION['persons'] = array();
}
if (isset($_POST)) {
$_SESSION['persons'][] = $_POST['val'];
}
foreach($_SESSION['persons'] as $d){
echo $d;
echo '</br>';
}
<form action="exam.php" method="post">
Enter a new Person: <input type="text" name = "val">
<input type="submit" name = "send">
</form>
I did not compile the code, check for syntax errors, but the procedure is right.
room.php
<input name = "room" type = "text" size="70"/>
Update
updateroom.php
<?php
mysql_connect('localhost','athirahhazira','1234');
mysql_select_db("dbcollege");
session_start();
$sql = "UPDATE studentsroom set room='$strroom' WHERE roomid='$_GET[roomid]'";
mysql_query($sql) or die('Error updating room status');
header('Location:staff/room-staff.php');
?>
i can update if there is a default value such as :
$sql = "UPDATE studentsroom set room='A206' WHERE roomid='$_GET[roomid]'";
but not the value from a textbox. could u help me with what i am missing here?
try this
$sql = "UPDATE studentsroom set room='A206' WHERE roomid='".$_REQUEST['roomid']."'";
Note: your code can be sql injection. also mysql_* is deprecated use mysqli_* or PDO
Update2:
add a form and submit button instead of hyperlink
<form method="post" action="updateroom.php" >
<input name = "room" type = "text" size="70"/>
<input type="hidden" name="roomid" value="<?php echo $row_Recordsetroomid['roomid'];?>" />
<input type="submit" name="submit" value="Update" />
</form>
AND update.php
<?php
mysql_connect('localhost','athirahhazira','1234');
mysql_select_db("dbcollege");
session_start();
$room = $_REQUEST['room'];
$roomid = $_REQUEST['roomid'];
$room = mysql_real_escape_string($room);
$roomid = mysql_real_escape_string($roomid);
$sql = "UPDATE studentsroom set room='$room' WHERE roomid='$roomid'";
mysql_query($sql) or die('Error updating room status');
header('Location:staff/room-staff.php');
?>
The quotes for the index in the $_GET is missing. Trying to access array variables like $array[key] instead of $array['key'], will trigger an error in most cases. So always try to use quotes for array indexes.
You can try with this.
$sql = "UPDATE studentsroom set room='A206' WHERE roomid='".$_GET['roomid']."'";
I'm trying to input some values in event.php and store them in an array ($mem). I'm then passing this array in another eventregister.php file where I'm inserting it in MySQL table.
Starting lines in my event.php file:
<?php
session_start();
$slug = $_GET['slug'];
$sess_uid = $_SESSION['id'];
$sess_email = $_SESSION['email'];
$sess_name = $_SESSION['name'];
if(isset($_POST['submit'])&&$_POST['submit']=='register1')
require_once('13/functions/eventregister.php');
?>
In <body>:
...
...
$result = mysql_query("SELECT * FROM event WHERE slug = '".$slug."'");
if ($result == true){
$row=mysql_fetch_assoc($result);
$id=$row['id'];
}
$_SESSION['eventid']=$id;
$_SESSION['eventname']=$row['name'];
$_SESSION['max_members']=$row['members'];
php $mem=array_fill(0,$row['members'],'');?>
<form action="" method="post">
<?php for ($i=0;$i<$row['members']-1;$i++){
echo '<label>TRYST ID of Member '.($i+1).' :</label>';
echo '<input type="text" size="20" name="'.$mem[$i].'"><br>';
}
echo '<button type="submit" id="submit" value="Register" name="register1">Register</button>';?>
</form>
My eventregister.php file:
<?php
session_start();
foreach($_POST['mem'] as $key=>$value){
$value=mysql_real_escape_string($value);
if(strlen($value)==0)
$value="Null";
}
$sess_uid = $_SESSION['id'];
$sess_email = $_SESSION['email'];
$sess_name = $_SESSION['name'];
$e_id=$_SESSION['eventid'];
$e_name=$_SESSION['eventname'];
$e_max_mem=$_SESSION['max_members'];
mysql_query("INSERT INTO eventregister(event_id,event_name,max_number) VALUES('".$e_id."','".$e_name."','".$e_max_mem."')");
$url="events.php?slug=".$slug;
header('Location: ' . $url);
exit;
?>
The page doesn't show any error, redirects are working, its just that no rows get affected in SQL. I'm still in the learning process, hence using the old notations of PHP.
If you look closely at the INSERT statement, you see you have a stray comma at the end. Remove that:
mysql_query("INSERT INTO eventregister(event_id,event_name,max_number)
VALUES('".$e_id."','".$e_name."','".$e_max_mem."')");
You could also see the MySQL error in your server logs. Or call mysql_error to find out more if a query fails.
I also do not see a mysql_connect anywhere.
Your commas and brackets not organised
mysql_query("INSERT INTO eventregister(event_id,event_name,max_number) VALUES('".$e_id."','".$e_name."','".$e_max_mem."')");
I have a following form:
<form action="doThis.php?warehouse=12" method="post">
<input name="field1" type="text" />
<input name="field2" type="text" />
</form>
And doThis.php:
$field1 = mysql_real_escape_string($_POST['field1'], $mysql);
$field2 = mysql_real_escape_string($_POST['field2'], $mysql);
$warehouse = $_GET['warehouse'];
if ( !someTableNameValidation($warehouse) ) {
someErrorHandling();
}
$qry = "INSERT INTO table".$warehouse." ( field1, field2 ) VALUES( '$field2', '$field2') ";
$result = #mysql_query($qry, $mysql);
As you can see, I'm using $_POST to get data from the form, and $_GET to get variable $warehouse which is used to indicate table number.
Can I use both $_POST & $_GET at the same time? Is this kind of usage correct?
Yes you could. $_GET['warehouse'] will be taken from the query string, $_POST variables from submitted POST values.
Yes, this is possible. But you could also use a hidden field:
<form action="doThis.php">
<input type="hidden" name="warehouse" value="12" />
<input name="field1" type="text" />
<input name="field2" type="text" />
Please be aware that your code is very vulnerable to sql injections!
Yes I always do that.
Also note you should never use mysql_query. Search for php PDO. Not to mention the awful # for suppressing error
Yes, however it should be:
$field1 = $_POST['field1'];
$field2 = $_POST['field2'];
$warehouse = $_GET['warehouse'];
$qry = "INSERT INTO table".$warehouse." ( field1, field2 ) VALUES ('".mysql_real_escape_string($field2)."', '".mysql_real_escape_string($field2)."')";
$result = #mysql_query($qry);
(Fixed syntax)
I frequently use POST and GET together, so that the PHP side can know whether it was a normal form submission or via AJAX.
<form action='dest.php'>
.
.
.
vs
ajaxSubmit( 'dest.php?a=1', ... );
<html>
<head><title>HEllo</title></head>
<body>
<input type="text" name="id">
<input type="text" name="name">
<input type="text" name="address">
<input type ="submit" name = "s" value = "Employee">
<?php
$link =mysql_connect('localhost','root') or die("Failed");
mysql_select_db("gagan",$link) or die("database not exists");
if($_POST['s']=="Employee")
{
print "g";
$id = mysql_real_escape_string($_POST['id']);
$name = $_POST['name'];
$address = $_POST['address'];
print "hi";
$update = "update emp set name = $name, address=$address where id = $id";
$result = mysql_query($update,$link);
print "Hello";
if($result)
{
print "Updated";
}
else{
print "$update";
}
}
?>
</body>
</html>
When i run this code it produce an notice and the above code is not working.
Notice: Undefined index: s in C:\wamp\www\1.php on line 12
What's the problem in my code can anybody tell me?
You forgot the form tag.
<form action="yourform.php" method="POST">
You need to ensure that array member is set first. Try using the result of isset($_POST['s']) to ensure it is set before trying to access it.
You need the form tage with the method set to post.
ie
The main problem (in addition to the missing form tag) is that the program flow continues to the part that tries to save the data even when the form hasn't been submitted yet. You must check that the form has been submitted before trying to save the data, or even easier would be if you moved the data saving part to its own script.
You also have an invalid SQL query but that's another matter :)