I want a html text input with name="input1" become a php variable called $value. I tried doing:
<html>
<form action="added.php" method="post" />
<p>Send a message to JANNES database: <input type="text" name="input1" />
</p>
<input type="submit" value"Submit" />
</form>
</html>
<?php
$value = $_POST["input1"];
$sql = "INSERT INTO janne (String) VALUES ('$value')";
if(!mysql_query($sql)){
die('vajsing: ' . mysql_error());
}
?>
And it is working, but I get this annoying error when I visit the website:
"Notice: Undefined index: input1 in C:\wamp\www\added.php on line 26
Call Stack
Time Memory Function Location
1 0.0008 250336 {main}( ) ..\added.php:0"
Notice that i didn't post the whole code. My php file is called added.php.
By default, $_POST["input1"] is empty as you have not submitted your form yet. You should check whether the form is submitted or not, like below:
if(isset($_POST["input1"])){
//rest of code
}
Check if the form is submitted or not. If you load the page directly, there's nothing inside $_POST['input1'].
<?php
if(isset($_POST['submit']))
{
$value = $_POST["input1"];
$sql = "INSERT INTO janne (String) VALUES ('$value')";
if(!mysql_query($sql)){
die('vajsing: ' . mysql_error());
}
}
?>
Change it to this:
<?php
if(isset($_POST["input1"])){
$value = $_POST["input1"];
$sql = "INSERT INTO janne (String) VALUES ('$value')";
if(!mysql_query($sql)){
die('vajsing: ' . mysql_error());
}
}
?>
The problem is: You weren't checking to see if it was set before trying to use it, so when you visit it without the post data being set then it gives you an error.
Related
When I save a form from html to php and finally store it in MySQL somewhere in that line it save the var= including what comes after the =
Here is my html:
<form action="searchResultsSave.php" method="POST">
What are we looking for? <input type="text" name="searchVar" />
<input type="submit" value="Submit">
</form>
Php:
$searchVar = file_get_contents('php://input');
$sql = "INSERT INTO g_information(searchVar) VALUES ('$searchVar')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
Finally my output in mysql is: "searchVar=cars" when it should just be "cars".
Where do you think I went wrong?
$searchVar = file_get_contents('php://input');
should be
$searchVar = $_POST['searchVar'];
This way you get the value of the search term.
You should read input variable from the form
<?php
$_POST["searchVar"];
?>
Then do some validation on the input, making sure no illegal characters are entered and data is safe to store in MySQL database
<?php
$_POST['searchVar'] = filter_var($_POST['searchVar'], FILTER_SANITIZE_STRING);
$sql = "INSERT INTO g_information(searchVar) VALUES ("'.$_POST['searchVar'].'")";
?>
im working on a project but first i would to understand one thing.
i have 2 input type text field with name="firstname[]" as an array (in the example im working with no jquery but it will be generated dinamically with it) and cant make it to mysql.
here is what i have: index.php
<html>
<body>
<form action="insert.php" method="post">
Firstname: <input type="text" name="firstname[]"> <br>
Firstname 2: <input type="text" name="firstname[]">
<input type="submit">
</form>
</body>
</html>
insert.php
<?php
$con=mysqli_connect("localhost","inputmultiplicad","inputmultiplicado","inputmultiplicado");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql="INSERT INTO input_field (firstname)
VALUES
('$_POST[firstname]')";
if (!mysqli_query($con,$sql))
{
die('Error: ' . mysqli_error($con));
}
echo "1 record added";
mysqli_close($con);
?>
the question is: how can i send the firstname[] array to the mysql database?
thanks for your time
This should work:
//Escape user input
$names = array_map('mysql_real_escape_string', $_POST[firstname]);
//Convert array to comma-delimited list
$names = rtrim(implode(',', $names), ',');
//Build query
$sql="INSERT INTO input_field (firstname) VALUES ('$names')";
Note: In general, it's better to use parameterized queries than mysql_real_escape_string(), but the latter is much safer than no escaping at all.
The following should generate the SQL statement you need. Remember to use mysql_escape_string before putting it into your database, though! Or even better, use PDO and bind the values. :)
$values = array();
$sql = "INSERT INTO table (firstname) VALUES ";
foreach ($_POST['firstname'] as $name) {
$values[] = "('".mysql_real_escape_string($name)."')";
}
$sql .= implode(",", $values);
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)
i wrote in file showList.php the following form, which select items from the database and show them in drop-down list:
<form id="selForm" name="selForm" action="index.php" method="post">
<select name="selection" id="selection">
<option id="nothingSelected" >--Choose form---></option>
<?php
$con=mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("myDatabase",$con);
$result = mysql_query("SELECT * FROM formsTable");
while($row = mysql_fetch_array($result))
{
$selection_id=$row['id'];
if($_POST['selection']==$selection_id)$selElement="selected";
echo "<option id='$selection_id' name=\"sectionid\" value='$selection_id' >";
echo $row['nummer'] . " " . $row['titel']. " ";
echo "</option>";
}
?>
</select>
<input type="button" value="load form" onClick="validateForm(document.selForm)">
<input type="button" value="delete form" onClick="deleteForm(document.selForm);">
</form>
I include this file in index.php as follows:
<?php include('showList.php');?>
Now when I call index.php, a list of found forms will be displayed in a drop-down list.
This works fine in firefox, my Problem is when I call index.php in internetexplorer, I get the following error:
Notice: Undefined index: selection in C:\path\showList.php on line 43
Line 43 is:
if($_POST['selection']==$selection_id)$selElement="selected";
as you can see in the form above.
Any idea?
You need to change the problem line from:
if($_POST['selection']==$selection_id)$selElement="selected";
to:
if(isset($_POST['selection']) && ($_POST['selection']==$selection_id))
$selElement="selected";
to check that a value for (as #b1onic suggested).
Obviously nothing will be POSTed the first time the form is shown in the browser - whichever browser you are using - so you will get that error.
Seems that your php script are trying to read the 'selection' in your $_POST variable but it wasn't defined yet.
Replace that line:
if($_POST['selection']==$selection_id)
To that:
if(array_key_exists('selection', $_POST) && $_POST['selection'] == $selection_id)
or
if(isset($_POST['selection']) && $_POST['selection'] == $selection_id)
This should make fix your warning and there are differences between the array_key_exists. In this case, use isset() because its faster and easier.
<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 :)