I'm trying to insert status update into my database in table called blabbing, but it's not working.
my php code
$thisRandNum = rand(9999999999999,999999999999999999);
$_SESSION['wipit'] = base64_encode($thisRandNum); // Will always overwrite itself each time this script runs
// ------- POST NEW BLAB TO DATABASE ---------
$blab_outout_msg = "";
if (isset($_POST['status']) && $_POST['status'] != "" && $_POST['status'] != " "){
$blabWipit = $_POST['blabWipit'];
$sessWipit = base64_decode($_SESSION['wipit']);
if (!isset($_SESSION['wipit'])) {
} else if ($blabWipit == $sessWipit) {
// End Delete any blabs over 20 for this member
$status = $_POST['status'];
$status = stripslashes($status);
$status = strip_tags($status);
$status = mysql_real_escape_string($status);
$status = str_replace("'", "'", $status);
$sql = mysql_query("INSERT INTO blabbing (mem_id, profile_id, the_blab, blab_date) VALUES('$logOptions_id','$logOptions_id','$status', now())");
$blab_outout_msg = "";
}
}
my html code
<div style="background-color:#f2f2f2; border:#ebebeb 1px solid; padding:8px;">
<form action="home.php" method="post" enctype="multipart/form-data" name="blab_from">
<textarea name="status" id="status" rows="3" style="width:99%;"></textarea>
<input name="submit" type="submit" class="btn" value="Blab" /> Limit: <script>displaylimit("","status",255)</script>
<input name="blabWipit" type="hidden" value="<? print $thisRandNum;?>" />
</form>
</div>
any help appreciated
$thisRandNum = rand(9999999999999,999999999999999999);
$_SESSION['wipit'] = base64_encode($thisRandNum); // Will always overwrite itself each time this script runs
// ------- POST NEW BLAB TO DATABASE ---------
$blab_outout_msg = "";
if (isset($_POST['status']) && $_POST['status'] != "" && $_POST['status'] != " "){
$blabWipit = $_POST['blabWipit'];
$sessWipit = base64_decode($_SESSION['wipit']);
// if (!isset($_SESSION['wipit'])) {
//} else if ($blabWipit == $sessWipit) {
// End Delete any blabs over 20 for this member
$status = $_POST['status'];
$status = stripslashes($status);
$status = strip_tags($status);
$status = mysql_real_escape_string($status);
$status = str_replace("'", "'", $status);
$sql = mysql_query("INSERT INTO blabbing (mem_id, profile_id, the_blab, blab_date) VALUES('$logOptions_id','$logOptions_id','$status', now())");
$blab_outout_msg = "";
// }
}
Try this if it works then else if ($blabWipit == $sessWipit) { this condition is not true
Related
This question already has answers here:
mysql update - skip blank fields?
(3 answers)
Closed 1 year ago.
Is there a way to update one or more field ignoring the empty fields in MySQL and PHP?
These are values that I need to enter in a HTML form:
<input type='text' placeholder='ID' name='ID' value='' id='ID'>
<input type='text' placeholder='Product name' name='prod_name' value=''>
<input type='text' placeholder='Description' name='prod_desc' value=''>
<input type='text' placeholder='Solde price' name='prod_s_price' value=''>
<input type='text' placeholder='Reg. price' name='prod_r_price' value=''>
<input type='text' placeholder='Product model' name='prod_mod' value=''>
When I submit the values, they are stored in those variables:
$prod_id = $_POST['ID']; //Int ID
$prod_name = $_POST['prod_name']; //String
$prod_desc = $_POST["prod_desc"]; //String
$prod_s_price = $_POST["prod_s_price"]; //Float
$prod_r_price = $_POST["prod_r_price"]; //Float
$prod_mod = $_POST["prod_mod"]; //String
When we send the POST request, it goes in a MySQL query:
if ($_POST['ID'] == null) {
header('Location: ../index.php?update=error_undefined');
} else {
$sql = "UPDATE app SET app_name='$prod_name', app_desc='$prod_desc', app_s_price=$prod_s_price, app_r_price=$prod_r_price, app_mod='$prod_mod' WHERE app_id=$prod_id;";
$result = mysqli_query($conn, $sql);
header('Location: ../index.php?update=succes');
}
Use the following, it will not arise any sort of issue.
if ($_POST['ID'] == null) {
header('Location: ../index.php?update=error_undefined');
} else {
$update_any = false;
$sql = "UPDATE app SET ";
$prod1 = null;
if (!empty($prod_name)) {
$prod1 = "app_name='$prod_name', ";
$update_any = true;
}
$prod2 = null;
if (!empty($prod_desc)) {
$prod2 = "app_desc='$prod_desc', ";
$update_any = true;
}
$prod3 = null;
if (!empty($prod_s_price)) {
$prod3 = "app_s_price='$prod_s_price', ";
$update_any = true;
}
$prod4 = null;
if (!empty($prod_r_price)) {
$prod4 = "app_r_price='$prod_r_price', ";
$update_any = true;
}
$prod5 = null;
if (!empty($prod_mod)) {
$prod5 = "app_mod='$prod_mod', ";
$update_any = true;
}
$notrimed_query = $prod1.$prod2.$prod3.$prod4.$prod5;
$trimed_query = rtrim($notrimed_query, ", ");
$query = $sql.$trimed_query." WHERE app_id=$prod_id;";
if ($update_any)
{
mysqli_query($conn, $query);
header('Location: ../index.php?update=succes');
}
else
{
echo "No Update Occur.";
}
}
Work juste fine that way! Just need to replace the PHP code to that! Thanks to me.
if ($_POST['ID'] == null) {
header('Location: ../index.php?update=error_undefined');
} else {
$sql = "UPDATE app SET ";
$prod1 = null;
if (!empty($prod_name)) {
$prod1 = "app_name='$prod_name', ";
}
$prod2 = null;
if (!empty($prod_desc)) {
$prod2 = "app_desc='$prod_desc', ";
}
$prod3 = null;
if (!empty($prod_s_price)) {
$prod3 = "app_s_price='$prod_s_price', ";
}
$prod4 = null;
if (!empty($prod_r_price)) {
$prod4 = "app_r_price='$prod_r_price', ";
}
$prod5 = null;
if (!empty($prod_mod)) {
$prod5 = "app_mod='$prod_mod', ";
}
$notrimed_query = $prod1.$prod2.$prod3.$prod4.$prod5;
$trimed_query = rtrim($notrimed_query, ", ");
$query = $sql.$trimed_query." WHERE app_id=$prod_id;";
mysqli_query($conn, $query);
header('Location: ../index.php?update=succes');
}
I`m trying to insert data into a database, from an excel file but starting from column 2 because the first column is being used for the name of the row in the table.
This is my form to input the file, this code reads from the first column, I want to start reading from column 2.
<h2>Import Excel File into MySQL Database using PHP</h2>
<div class="outer-container">
<form action="import.php" method="post"
name="frmExcelImport" id="frmExcelImport" enctype="multipart/form-data">
<div>
<label>Choose Excel
File</label> <input type="file" name="file"
id="file" accept=".xls,.xlsx">
<button type="submit" id="submit" name="import"
class="btn-submit">Import</button>
</div>
</form>
</div>
<div id="response" class="<?php if(!empty($type)) { echo $type . " display-block"; } ?>"><?php if(!empty($message)) { echo $message; } ?></div>
and this is the action file:
<?php
include 'koneksi/koneksi.php';
require_once('vendor/php-excel-reader/excel_reader2.php');
require_once('vendor/SpreadsheetReader.php');
if (isset($_POST["import"]))
{
$allowedFileType = ['application/vnd.ms-excel','text/xls','text/xlsx','application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];
if(in_array($_FILES["file"]["type"],$allowedFileType)){
$targetPath = 'uploads/'.$_FILES['file']['name'];
move_uploaded_file($_FILES['file']['tmp_name'], $targetPath);
$Reader = new SpreadsheetReader($targetPath);
$sheetCount = count($Reader->sheets());
for($i=0;$i<$sheetCount;$i++)
{
$Reader->ChangeSheet($i);
foreach ($Reader as $Row )
{
$id_koperasi = "";
if(isset($Row[0])) {
$id_koperasi = mysqli_real_escape_string($con,$Row[0]);
}
$nama_koperasi = "";
if(isset($Row[1])) {
$nama_koperasi = mysqli_real_escape_string($con,$Row[1]);
}
$alamat = "";
if(isset($Row[2])) {
$alamat = mysqli_real_escape_string($con,$Row[2]);
}
$telp = "";
if(isset($Row[3])) {
$telp = mysqli_real_escape_string($con,$Row[3]);
}
$hp = "";
if(isset($Row[4])) {
$hp = mysqli_real_escape_string($con,$Row[4]);
}
$nama_cp = "";
if(isset($Row[5])) {
$nama_cp = mysqli_real_escape_string($con,$Row[5]);
}
$email = "";
if(isset($Row[6])) {
$email = mysqli_real_escape_string($con,$Row[6]);
}
$tanggal_fu = "";
if(isset($Row[7])) {
$tanggal_fu = mysqli_real_escape_string($con,$Row[7]);
}
$ket_fu = "";
if(isset($Row[8])) {
$ket_fu = mysqli_real_escape_string($con,$Row[8]);
}
$hasil_pembahasan = "";
if(isset($Row[9])) {
$hasil_pembahasan = mysqli_real_escape_string($con,$Row[9]);
}
$status = "";
if(isset($Row[10])) {
$status = mysqli_real_escape_string($con,$Row[10]);
}
$provinsi = "";
if(isset($Row[11])) {
$provinsi = mysqli_real_escape_string($con,$Row[11]);
}
$kota = "";
if(isset($Row[12])) {
$kota = mysqli_real_escape_string($con,$Row[12]);
}
$kec = "";
if(isset($Row[13])) {
$kec = mysqli_real_escape_string($con,$Row[13]);
}
$kel = "";
if(isset($Row[14])) {
$kel = mysqli_real_escape_string($con,$Row[14]);
}
$rt = "";
if(isset($Row[15])) {
$rt = mysqli_real_escape_string($con,$Row[15]);
}
$rw = "";
if(isset($Row[16])) {
$rw = mysqli_real_escape_string($con,$Row[16]);
}
$jln = "";
if(isset($Row[17])) {
$jln = mysqli_real_escape_string($con,$Row[17]);
}
$kodep = "";
if(isset($Row[18])) {
$kodep = mysqli_real_escape_string($con,$Row[18]);
}
if (!empty($id_koperasi) || !empty($nama_koperasi) || !empty($alamat) || !empty($telp) || !empty($hp) || !empty($nama_cp) || !empty($email) || !empty($tanggal_fu) || !empty($ket_fu) || !empty($hasil_pembahasan) || !empty($status) || !empty($provinsi) || !empty($kota) || !empty($kec) || !empty($kel) || !empty($rt) || !empty($rw) || !empty($jln) || !empty($kodep) ) {
$query = "INSERT INTO t_koperasi(id,id_koperasi,nama_koperasi,alamat,telp,hp,nama_cp,email,tanggal_fu,ket_fu,hasil_pembahasan,status,provinsi,kota,kec,kel,rt,rw,jln,kodep) VALUES ('',
'$id_koperasi',
'$nama_koperasi',
'$alamat',
'$telp',
'$hp',
'$nama_cp',
'$email',
'$tanggal_fu',
'$ket_fu',
'$hasil_pembahasan',
'$status',
'$provinsi',
'$kota',
'$kec',
'$kel',
'$rt',
'$rw',
'$jln',
'$kodep')" or die(mysqli_error($con));
;
$result = mysqli_query($con, $query);
if (! empty($result)) {
$type = "success";
$message = "SUKSES";
} else {
$type = "error";
$message = "Problem in Importing Excel Data";
}
}
}
}
}
else
{
$type = "error";
$message = "Invalid File Type. Upload Excel File.";
}
}
include 'views/v_import.php'
?>
I already tried to put $col = 1 but it still wont work, I tried using the for=i but it still will not work.
a possible solution with an extra counter
$Reader->ChangeSheet($i);
$number = 0;
foreach ($Reader as $Row )
{
if ($number!=0) {
......
}
$number++;
}
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 have a basic Form that submits data into a database and I want it to require certain fields to be submitted, so far it recongizes that the fields are empty, but it still submits regardless. I can't seem to find a solution..
Code
<?
// define variables and set to empty values
$asinErr = $qtyErr = $floorErr = $locErr;
$asin = $quantity = $floor = $location;
# this is processed when the form is submitted
# back on to this page (POST METHOD)
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
if (empty($_POST["asin"]))
{$asinErr = "ASIN is required";}
else
{$asin = addslashes($_POST["asin"]);}
if (empty($_POST["quantity"]))
{$qtyErr = "Quantity is required";}
else
{$quantity = addslashes($_POST["quantity"]);}
if (empty($_POST["floor"]))
{$floorErr = "Floor is required";}
else
{$floor = addslashes($_POST["floor"]);}
if (empty($_POST["location"]))
{$locErr = "Location is required";}
else
{$location = addslashes($_POST["location"]);}
# setup SQL statement
$sql = " INSERT INTO kiva_amnesty_log ";
$sql .= " (asin, quantity, floor, location, date) VALUES ";
$sql .= " ('$asin','$quantity','$floor','$location', now()) ";
#execute SQL statement
$result = mysql_query($sql, $cid);
# check for error
if (mysql_error()) { print "Database ERROR: " . mysql_error(); }
print "<h3><font color=red>New Amnesty Added - View it <a href=amnesty_log_summary.php>HERE</a></font></h3>";
}
?>
<form name="fa" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>" method="POST">
<table>
<tr><td>ASIN:</td><td><input type="text" name="asin" id="asin"><span class="error">* <?php echo $asinErr;?></span></td></tr>
<tr><td>Quantity:</td><td><input type="text" name="quantity" id="quantity"><span class="error">* <?php echo $qtyErr;?></span></td></tr>
<tr><td>Floor:</td><td><select name="floor"><option value="1">Floor 1</option><option value="2">Floor 2</option></select><span class="error">* <?php echo $floorErr;?></span></td></tr>
<tr><td>KIVA Floor:</td><td><input type="radio" value="Yes" name="location">Yes<input type="radio" value="No" name="location">No</select><span class="error">* <?php echo $locErr;?></span></td></tr>
<tr><td><input type="submit" name="submit" id="submit" value="Submit Amnesty!"></td></tr>
</table>
</form>
Updated:
<?
// define variables and set to empty values
$asinErr = $qtyErr = $floorErr = $locErr = "";
$asin = $quantity = $floor = $location = "";
$lb_error = 0;
if ($_SERVER['REQUEST_METHOD'] == "POST")
{
if (empty($_POST["asin"])) {
$asinErr = "ASIN is required";
$lb_error = 1;
} else {
$asin = addslashes($_POST["asin"]);
}
if (empty($_POST["quantity"])) {
$qtyErr = "Quantity is required";
$lb_error = 1;
} else {
$quantity = addslashes($_POST["quantity"]);
}
if (empty($_POST["floor"])) {
$floorErr = "Floor is required";
$lb_error = 1;
} else {
$floor = addslashes($_POST["floor"]);
}
if (empty($_POST["location"])) {
$locErr = "Location is required";
$lb_error = 1;
} else {
$location = addslashes($_POST["location"]);
}
if($lb_error) {
continue;
}
# setup SQL statement
$sql = " INSERT INTO kiva_amnesty_log ";
$sql .= " (asin, quantity, floor, location, date) VALUES ";
$sql .= " ('$asin','$quantity','$floor','$location', curdate()) ";
#execute SQL statement
$result = mysql_query($sql, $cid);
# check for error
if (mysql_error()) { print "Database ERROR: " . mysql_error(); }
You want to check if you error variables are empty. If they are not, then break the script
ie
if(!empty($asinErr) || !empty($qtyErr) || !empty($floorErr) || !empty($locErr) ) {
break;
}
Something along these lines.
Check for the errors before you get to the point where you are writing to the database
Define at the top
$lb_error = 0;
Throughout your if/else checks for errors, if there is an error, assign the variable a 1
if (empty($_POST["asin"])) {
$asinErr = "ASIN is required";
$lb_error = 1;
} else {
$asin = addslashes($_POST["asin"]);
}
Then after you have completed all of these, do a check for errors and break if there are any
if($lb_error) {
break;
}
I'm developing a simple PHP website. I have a form for updating values. When that form is accessed via PHP(website) , It should have previous values, But it hasn't. How to do that ?
Because, Otherwise, It shows all fields empty and when one or two are updated, Other gets updated as empty. Please help !
<?php
require_once('../includes/config.php');
require_once('../includes/functions.php');
require_once('../includes/session.php');
require_once('../includes/database.php');
require_once('../includes/user.php');
require_once('../includes/photograph.php');
if (!$session->is_logged_in()) { redirect_to("login.php"); }
?>
<?php
// START FORM PROCESSING
if (isset($_POST['submit'])) { // Form has been submitted.
$errors = array();
// perform validations on the form data
$required_fields = array('lives_in', 'belongs_to', 'college', 'works_at', 'grade', 'grade2', 'hobbies', 'zodiac', 'phone_no', 'facebook', 'company', 'bio');
$errors = array_merge($errors, check_required_fields($required_fields, $_POST));
$lives_in = trim($database->escape_value($_POST['lives_in']));
$belongs_to = trim($database->escape_value($_POST['belongs_to']));
$college = trim($database->escape_value($_POST['college']));
$works_at = trim($database->escape_value($_POST['works_at']));
$grade = trim($database->escape_value($_POST['grade']));
$grade2 = trim($database->escape_value($_POST['grade2']));
$hobbies = trim($database->escape_value($_POST['hobbies']));
$zodiac = trim($database->escape_value($_POST['zodiac']));
$phone_no = trim($database->escape_value($_POST['phone_no']));
$facebook = trim($database->escape_value($_POST['facebook']));
$company = trim($database->escape_value($_POST['company']));
$bio = trim($database->escape_value($_POST['bio']));
if (empty($errors) ) {
$query = "UPDATE users
SET lives_in = '{$lives_in}',
belongs_to = '{$belongs_to}',
college = '{$college}' ,
works_at = '{$works_at}',
grade = '{$grade}',
grade2 = '{$grade2}',
hobbies = '{$hobbies}',
zodiac = '{$zodiac}',
phone_no = '{$phone_no}',
facebook = '{$facebook}',
company = '{$company}',
bio = '{$bio}'
WHERE id = 23
LIMIT 1";
$result = mysql_query($query, $database->connection);
if ($result) {
$message = "Account Updated";
} else {
$message = mysql_error();
}
} else {
if (count($errors) == 1) {
$message = mysql_error();;
} else {
$message = "There were " . count($errors) . " errors in the form.";
}
}
} else { // Form has not been submitted.
$lives_in = "";
$belongs_to = "";
$college = "";
$works_at = "";
$grade = "";
$grade2 = "";
$hobbies = "";
$zodiac = "";
$phone_no = "";
$facebook = "";
$company = "";
$bio = "";
}
?>
It's very easy, just print out the previous values in the form when you create it with php.
For example:
<input type="text" name="field_name" value="<?php echo $previous_value;?>" />
Or, if it's a select, let's say:
<select name="field_name">
<?php foreach($posible_values as $value){ ?>
<option value="<?php echo $value;?>"
<?php echo ($value==$previous_value)?'selected="selected"':'';?> >
<?echo $value?>
</option>
<?php } ?>
</select>