So new here to stack exchange but here goes nothing. So when I send a form to my apache server my data is only showing up as ones and zeros. Using var_dump[_$POST]; shows all of my data is correct before passing to MySQL.
My html form:
<form method="POST" action="submit.php" class="subForm">
<input type="text" name = "item1" value="0">
<input type="text" name="item2" value="0">
<input type="text" name="item3" value="no">
<input type="text" name="item4" value="no">
<input type="text" name="item5" value="no">
<input type="text" name="item6" value="no">
<input type="submit" id = "form2">
</form>
my php:
$connect = mysqli_connect('*****','*****','*****','*****');
if(!$connect){
die('Could not Connect: ' . mysqli_error($connect));
}
$NoR = isset($_POST["item1"]);
$CC = isset($_POST["item2"]);
$SD = isset($_POST["item3"]);
$HD = isset($_POST["item4"]);
$pack1 = isset($_POST["item5"]);
$pack2 = isset($_POST["item6"]);
$sql = "INSERT INTO form_test (item_1,item_2,item_3,item_4,item_5,item_6) VALUES (".$NoR.",".$CC.",".$SD.",".$HD.",".$pack1.",".$pack2.")";
mysqli_query($connect, $sql);
mysqli_close($connect);
var_dump($_POST)
var_dumb shows all data input is correct but in the table itself everything shows as 1s and 0s. Any advice?
In php, isset will return a boolean value (true if it's set, false if not), and when you try to print a boolean in php, it will display as 1 or 0. I suggest using the ternary comparison in your code, it's shorter and more readable than having a ton of if statements:
$NoR = isset($_POST["item1"]) ? $_POST["item1"] : '';
$CC = isset($_POST["item2"]) ? $_POST["item2"] : '';
$SD = isset($_POST["item3"]) ? $_POST["item3"] : '';
$HD = isset($_POST["item4"]) ? $_POST["item4"] : '';
$pack1 = isset($_POST["item5"]) ? $_POST["item5"] : '';
$pack2 = isset($_POST["item6"]) ? $_POST["item6"] : '';
Add this in your HTML form
<input type="text" name="action" value="submit">
And PHP code will be
if (isset($_POST['action']) && ($_POST['action']) == 'submit') {
$NoR = mysqli_real_escape_string($_POST["item1"]);
$CC = mysqli_real_escape_string($_POST["item2"]);
$SD = mysqli_real_escape_string($_POST["item3"]);
$HD = mysqli_real_escape_string($_POST["item4"]);
$pack1 = mysqli_real_escape_string($_POST["item5"]);
$pack2 = mysqli_real_escape_string($_POST["item6"]);
$sql = "INSERT INTO form_test (item_1,item_2,item_3,item_4,item_5,item_6) VALUES (".$NoR.",".$CC.",".$SD.",".$HD.",".$pack1.",".$pack2.")";
mysqli_query($connect, $sql);
mysqli_close($connect);
//var_dump($_POST)
}
isset will only give you if value is set or not. 1 if set else 0.
Use
if(isset($_POST["item1"])){
$NoR = $_POST["item1"];`
}
Updated: I made code for you to make sude ISSET values only goto INSERT query!
$arrColumns = $arrValues = array();
foreach($_POST as $key=>$value){
$arrColumns[] = key($key);
$arrValues[] = $value;
}
if(is_array($arrValues)){
$sql = "INSERT INTO form_test (implode(',',$arrColumns))
VALUES(implode(',',$arrValues)";
mysqli_query($connect, $sql);
}
mysqli_close($connect);
Related
I have two pages, edit.php and editdone.php.
On the edit.php I am able to fill information, which is being sent to editdone.php. That page is then running a query that updates data in the mysql database.
The problem is; if I leave an input field on edit.php empty, editdone.php will then replace the current information in the database with empty data(nothing).
What I want to do is to make the editdone.php update data if something was written in the fields of edit.php. So if I choose to leave some fields empty and for example only fill one field in the form, I want to only update the filled fields with the filled data and NOT replace the not filled field with empty data. Those field should then, if I haven't filled any data in edit.php, keep the already existing data.
edit.php
<?php
if (!empty($error_msg)) {
echo $error_msg;
}
$cn = $_POST['cname'];
?>
<form action="editdone.php" method="POST" enctype="multipart/form-data" name="editdone" onsubmit="return validateForm()">
<input type="hidden" name="namec" value="<?php echo htmlspecialchars($cn); ?>">
<br>
Fyll i Företagets namn: <br>
<input type="text" name="company_name" id="company_name">
<br><br>
Lägg till en logga:
<input type="file" name="image" id="image">
<br><br>
Description:<br>
<textarea name="description" id="description" rows="4" cols="50"></textarea>
<br>
<br>
Fyll i välkomnings meddelande:<br>
<textarea name="welcome_text" id="welcome_text" rows="5" cols="50"></textarea>
<br>
<br>
Fyll i ett tack meddelande:<br>
<textarea name="thanks_message" id="thanks_message" rows="5" cols="50"></textarea>
<br>
<br>
<input type="submit" name="submit" value="Nästa" />
</form>
editdone.php
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
if(mysqli_connect_errno())
{
echo mysqli_connect_error();
}
$namenamec = $_POST['namec'];
$company_name = $_POST['company_name'];
$description = $_POST['description'];
$welcome_text = $_POST['welcome_text'];
$thanks_message = $_POST['thanks_message'];
$image = addslashes (file_get_contents($_FILES['image']['tmp_name']));
$logo = getimagesize($_FILES['image']['tmp_name']);
$image_type = $logo['mime'];
$q = "UPDATE project SET project_name='$company_name', description='$description', image='$image', image_type='$image_type', welcome_text='$welcome_text', thanks_message='$thanks_message' WHERE project_name='$namenamec' ";
$r = mysqli_query($mysqli,$q);
if($r)
{
echo "<br>Information stored successfully";
}
?>
For every input/textarea in edit.php, insert a <input type="hidden" value="company_name_old> etc... with the previous value. Then in editdone.php, check if the value in POST is empty or not.
<?php
$company_name = $_POST['company_name'];
if($company_name==""){
$company_name=$_POST['company_name_old'];
}
...
?>
1 cheap "hack" is to assign the current value of the field to the value of the input field and then concat the two strings or values together then save that var. to the database.
<?php
ini_set('display_errors',1);
error_reporting(E_ALL);
if(mysqli_connect_errno())
{
echo mysqli_connect_error();
}
$company_name = "";
$description = "";
$welcome_text = "";
$thanks_message = "";
$image = "";
$logo = "";
$image_type = "";
$namenamec = $_POST['namec'];
$company_name = $_POST['company_name'];
$description = $_POST['description'];
$welcome_text = $_POST['welcome_text'];
$thanks_message = $_POST['thanks_message'];
if( isset($_FILES) )
{
if( !empty($_FILES) )
{
if( isset($_FILES['image']['tmp_name']) )
{
if( $_FILES['image']['tmp_name'] != "" && !empty($_FILES['image']['tmp_name']) )
{
$image = addslashes (file_get_contents($_FILES['image']['tmp_name']));
if( $image != "" && !empty($image) )
{
$logo = getimagesize($_FILES['image']['tmp_name']);
$image_type = $logo['mime'];
}
}
}
}
}
$update_values = array();
if($company_name != "")
$update_values[] = "project_name='".$company_name."'";
if($description != "")
$update_values[] = "description='".$description."'";
if($image != "")
$update_values[] = "image='".$image."'";
if($image_type != "")
$update_values[] = "image_type='".$image_type."'";
if($welcome_text != "")
$update_values[] = "welcome_text='".$welcome_text."'";
if($thanks_message != "")
$update_values[] = "thanks_message='".$thanks_message."'";
$update_values_imploded = implode(', ', $update_values);
if( !empty($update_values) )
{
$q = "UPDATE project SET $update_values_imploded WHERE project_name='$namenamec' ";
$r = mysqli_query($mysqli,$q);
if($r)
{
echo "<br>Information stored successfully";
}
}
?>
Try replacing your query like this.
$q = "UPDATE project SET ";
$q .= $company_name ? "project_name='$company_name', " : "";
$q .= $description ? "description='$description', " : "";
$q .= $image ? "image='$image'," : "";
... so on(all fields)
$q .= "WHERE project_name='$namenamec'";
Make sure you remove , for last value
you can do like this here i have made only one variable you can check for each posted variable and append the $q variable as on
$q = "UPDATE project SET";
if(isset($_POST['namec']) && $_POST['namec']!=""){
$q.=" project_name='".$_POST['namec']."' ,";
}
$q=rtrim(',',$q);
$q.="WHERE project_name=".$namenamec;
I'm making admin panel so user can edit text of the page by this helppppp please... the data on webpage is displayed through the database here im giving him option to edit data and update it this will open data in textbox and when user clicks botton update it will update data in database but problem is that when click on update the form is not passing values in the text feild it is passing value "1" i dont know from where it is getting "1" if i echo posted veriable it shows 1
<?php
$query2 = mysqli_query($con, "SELECT * FROM home");
$row = mysqli_fetch_array($query2);
$pic1 = ucfirst($row['pic1']);
$pic2 = ucfirst($row['pic2']);
$pic3 = ucfirst($row['pic3']);
$pic4 = ucfirst($row['pic4']);
$pic5 = ucfirst($row['pic5']);
$pic6 = ucfirst($row['pic6']);
$pic7 = ucfirst($row['pic7']);
$pic8 = ucfirst($row['pic8']);
$par1 = ucfirst($row['par1']);
$par1pic = ucfirst($row['par1pic']);
$par2 = ucfirst($row['par2']);
$side_pic1 = ucfirst($row['side_pic1']);
$side_pic2 = ucfirst($row['side_pic2']);
?>
<form method="POST" action="update_home.php">
Paragraph no 1:
<textarea name="comment" rows="5" cols="40"><?php echo $par1; ?></textarea>
First name:
<input type="text" name="fname"><br>
<input type="submit" name="nw_update" value="Update_1"/><br><br>
</form>
<form method="POST" action="update_home.php">
Paragraph no 2:
<textarea name="comment1" rows="5" cols="40"><?php echo $par2; ?></textarea>
enter code here
<input type="submit" name="nw_update" value="Update_2"/><br><br>
</form>
<?php
include("dbconnection.php");
$value = isset($_POST["nw_update"]);
$fname = isset($_POST["fname"]);
echo $fname;
echo $value;
if ($value == "Update_1") {
$par = isset($_POST["comment"]);
$query2 = mysqli_query($con, "UPDATE home SET par1='$par' where id='1'");
}
if ($value == "Update_2") {
$par2 = isset($_POST["comment1"]);
$query2 = mysqli_query($con, "UPDATE home SET par2='$par2' where id='1'");
}
header("location : edit_home.php");
?>
Your problem is the use of isset() in for example:
$value = isset($_POST["nw_update"]);
isset() returns a boolean and when it is true and you cast it to a string, you get 1.
If you want to set its value depending on whether a POST request was made, you should use something like:
$value = isset($_POST["nw_update"]) ? $_POST["nw_update"] : 'default value (can be an empty string or NULL or something from the database)';
Now the value of your variable will contain either the posted value or the default value.
The following function works fine despite having the "Undefined offset" error but i need to debug it still. Can someone help me spot where the error is?
This is the form :
$query = "SELECT * FROM application WHERE Shortlist_status = 1 AND Interview_datetime != '' AND Email_checked ='' ORDER BY Candidate_id ASC";
$result = mysqli_query($link, $query) or die(mysqli_error($link));
while ($row = mysqli_fetch_array($result)) {
<input type="hidden" name="can_id[]" value=<?php echo $canid ?>>
<input type="hidden" name="job_id[]" value=<?php echo $jobid ?>>
(Some codes)
<input name="email[]" id="id" type="checkbox" value="1">
}
This is the form handling:
foreach ($_POST['can_id'] as $i => $candidate_id) {
$job_id = $_POST['job_id'][$i];
$email = $_POST['email'][$i];
$insertQuery = "UPDATE application SET Email_checked = '$email' WHERE Candidate_id = $candidate_id AND Job_id = $job_id";
$inserted = mysqli_query($link, $insertQuery) or die(mysqli_error($link));
}
if($inserted)
{
$message = 'Application successfully update <br>Manage Candidate';;
}
else
{
$message = 'Application failed <br>Manage Candidate';
}
echo $message;
Based on how you described the error in your comments, I would guess that the problem can be solved like this:
$email = isset($_POST['email'][$i]) ? $_POST['email'][$i] : "otherValue";
Replace "otherValue" with whatever you need if it is not set (probably "0")
$email = isset($request->email[$i]) ? $request->email[$i] : 0;
I'm trying to create a system that when i submit the form, after the page refresh it should show the new values that i get from the database. The values work well when they go into the databse but they dont show after submited, only when i refresh again. Thanks for helping
<?php
include("connect.php");
$query = "SELECT * FROM `laliga`";
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
$id = $row['id'];
$home = $row['home'];
$away = $row['away'];
$win = $row['win'];
$draw = $row['draw'];
$lose = $row['lose'];
}
echo "<h2>La Liga</h2>";
echo $home, " - ", $away;
if (isset($_POST) && $_POST['laliga'] == 1){
$select = mysql_real_escape_string($_POST['laliga']);
$select = $win + $select;
mysql_query("UPDATE laliga SET win='$select'");
}else if (isset($_POST) && $_POST['laliga'] == 'X'){
$select = mysql_real_escape_string($_POST['laliga']);
$select = '1';
$select = $draw + $select;
mysql_query("UPDATE laliga SET draw='$select'");
}else if (isset($_POST) && $_POST['laliga'] == 2){
$select = mysql_real_escape_string($_POST['laliga']);
$select = '1';
$select = $lose + $select;
mysql_query("UPDATE laliga SET lose='$select'");
}
header('Location: ../laliga.php');
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="radio" name="laliga" value="1">1
<input type="radio" name="laliga" value="X">X
<input type="radio" name="laliga" value="2">2
<input type="submit" name="submit" value="Submit"/>
</form>
<br/>
<?php
echo $home, " -> ", $win;
echo "<br/>Barazim -> ", $draw,"<br/>";
echo $away, " -> ", $lose;
?>
You should handle all of the post data at the top of the PHP file, whilst the header function will solve your problem it's a silly and inefficient way of approaching it. by handling the post data and updating the database first, by the time you query the database the data is there! at the moment you are trying to find the data and then adding it. does this make sense?
Good luck!
Add:
header('Location: <mypage.php>');
After mysql_query.
I have a page called index.html which takes in a few variable from the user, when the user submits them, it goes to the next page (result.php) On results.php the variables from the form on the index.html are posted. The results.php connects to database runs a query etc. All of this works fine. the results.php looks something like this:
<?php
$h = "localhost";
$u = "root";
$p = "******";
$d = "********";
//connectipn to mysql
$conn = mysql_connect( $h, $u, $p );
//connection to database
mysql_select_db($d);
$code = $_POST['code'];
$usage = $_POST['usage'];
$days = $_POST['days'];
$value = $_POST['value];
$sql_runners_up = "SELECT code
from some_table
where number = $value;"
$plans['p_h10'] = array();
$rs2 = mysql_query( $sql_runners_up, $conn );
while ( $row = mysql_fetch_array( $rs2 ))
{
$plans['p_h10'][] = $row["p_h10"];
}
?>
The results.php also has a form that the user can update some fields that are used in the query. the form that I currently have looks like this...
<form action="results.php" name="filter" method="post">
usage:<input name="" type="search" value="" /><br/>
days:<input name="" type="search" value="" /><br/>
<input name="" type="checkbox" value="" />value<br />
<input type="submit" value="submit" name="submit" />
</form>
Even with a blank as soon as the user submits the form I get an error (i have tried filling out the form to use real variable but still get the same error)
Warning: mysql_fetch_array(): supplied argument is not a valid MySQL
result resource
the error refers to the line around the while statement. my guess is that some variable/s are not being passed after the form is submitted or the entire results.php form is not being submitted / processed by the server. But to be honest I am really not sure. Any help will be greatly appreciated.
That's because mysql_query returned false.
Also, in your code, you've got a syntax error
$value = $_POST['value'];
Also, your code is vulnerable. Don't trust user's input!
so, correct code would be
$value = intval($_POST['value']);
Try:
$value = (int) $value;
$sql_runners_up = "SELECT code from some_table where number = $value";
$plans['p_h10'] = array();
$rs2 = mysql_query( $sql_runners_up);
try with this:
$sql_runners_up = "SELECT * from some_table where number = '$value';"
and also check that the $value has containing the value..
EDIT: in the above code i use * which select all the fields, cause that is see in your code you did:
$sql_runners_up = "SELECT code from some_table where number = $value;"
and in fetch query you are calling to field name p_h10:
while ( $row = mysql_fetch_array( $rs2 ))
{
// didn't selected $row["p_h10"] in your query
$plans['p_h10'][] = $row["p_h10"];
}
First of all, this row is invalid:
$value = $_POST['value];
It should be $_POST['value'];
But, this error occurs usually when there is an error in your query, or the connection is bad. Check your mysql_connect() & mysql_select_db() settings, it is probably bad.
looks like you did typos...
<?php
$h = "localhost";
$u = "root";
$p = "******";
$d = "********";
//connectipn to mysql
$conn = mysql_connect( $h, $u, $p );
//connection to database
mysql_select_db($d);
$code = $_POST['code'];
$usage = $_POST['usage'];
$days = $_POST['days'];
$value = $_POST['value']; // here
$sql_runners_up = "SELECT code
from some_table
where number = $value"; // and here
$plans['p_h10'] = array();
$rs2 = mysql_query( $sql_runners_up, $conn );
while ( $row = mysql_fetch_array( $rs2 ))
{
$plans['p_h10'][] = $row["p_h10"];
} ?>
also your form is not POSTing parameters... because name attributes are not set
<form action="results.php" name="filter" method="post">
usage:<input name="usage" type="search" value="" /><br/>
days:<input name="days" type="search" value="" /><br/>
<input name="value" type="checkbox" value="" />value<br />
<input type="submit" value="submit" name="submit" />
</form>