i want to update my data from database but unfortunately i can't be update.
there is anyone can help me.
i will be appreciated.
the problem is error can not show when my code does wrong.
sorry for my bad english.
here is my code
<?
include "new.php";
$response = array("updated data", 1);
if (isset($_POST['ID_Person']) && isset($_POST['FirstName']) && isset($_POST['MiddleName']) && isset($_POST['LastName']) && isset($_POST['AliasName'])
&& isset($_POST['Gender']) && isset($_POST['CityBirth']) && isset($_POST['DateBirth']) && isset($_POST['MonthBirth']) && isset($_POST['YearBirth']))
{
$id = $_POST['ID_Person'];
$name = $_POST['FirstName'];
$middle = $_POST['MiddleName'];
$last = $_POST['LastName'];
$alias = $_POST['AliasName'];
$gender = $_POST['Gender'];
$citybirth = $_POST['CityBirth'];
$datebirth = $_POST['DateBirth'];
$monthbirth = $_POST['MonthBirth'];
$yearbirth = $_POST['YearBirth'];
$hasil = sqlsrv_query($conn,"UPDATE T_Person SET
First_Name_Person = '$name' ,
Middle_Name_Person = '$middle' ,
Last_Name_Person = '$last' ,
Alias_Person = '$alias',
Gender_Person = '$gender',
City_Birth_Person = '$citybirth',
Date_Birth_Person = '$datebirth',
Month_Birth_Person = '$monthbirth',
Year_Birth_Person = '$yearbirth',
WHERE ID_Person = '$id'"
);
$rows_affected = sqlsrv_rows_affected($hasil);
if ($rows_affected === false)
{
die( print_r( sqlsrv_errors(), true));
}
if ($hasil)
{
$response["success"] = 1;
$response["message"] = "Product successfully edit.";
// echoing JSON response
echo json_encode($response);
}
}
else {
echo 'Data fail update';
}
?>
You have an error in your query with comma juste before WHERE
$hasil = sqlsrv_query($conn,"UPDATE T_Person SET First_Name_Person = '$name' ,
Middle_Name_Person = '$middle' ,
Last_Name_Person = '$last' ,
Alias_Person = '$alias',
Gender_Person = '$gender',
City_Birth_Person = '$citybirth',
Date_Birth_Person = '$datebirth',
Month_Birth_Person = '$monthbirth',
Year_Birth_Person = '$yearbirth',
no comma here ^
WHERE ID_Person = '$id'");
Try to echo your query in the PHP and copy paste this query in phpMyAdmin.
i guess there is no T_Person with ID_Person == $id in your table so nothing is updated
Please remove comma after this,
Year_Birth_Person = '$yearbirth'
Related
im making an insert function on php, everything is ok, there is no error but the data doesnt show up in the database, here is my php file
<?php
include 'koneksi/koneksi.php';
if(isset($_POST['Submit'])) {
$id_koperasi = $_POST['id'];
$nama_koperasi = $_POST['nama'];
$alamat = $_POST['alamat'];
$telp = $_POST['telp'];
$hp = $_POST['hp'];
$nama_cp = $_POST['kontak'];
$email = $_POST['email'];
$nama_cp = $_POST['kontak'];
$tanggal = $_POST['tgl'];
$ket_fu = $_POST['ket'];
$hasil_pembahasan = $_POST['hasil'];
$status = $_POST['stat'];
$query = "INSERT INTO t_koperasi(id,id_koperasi,nama_koperasi,alamat,telp,hp,nama_cp,email,tanggal_fu,ket_fu,hasil_pembahasan,status) VALUES ('',
'$id_koperasi',
'$nama_koperasi',
'$alamat',
'$telp',
'$hp',
'$nama_cp',
'$tanggal',
'$ket_fu',
'$hasil_pembahasan',
'$status')"
;
if (mysqli_query($con,$query)) {
header("location:index.php");
}else {
error_log( "This code has errors!" );
}
}
include 'views/v_form.php';
?>
and this is my database t_koperasi structure
The id is auto increasing, so you should remove id column in you insert sql. Also, the method you are using is dangerous, you should not totally trust what pass to you by other users in you website page, instead, you need to add filter functions.
You Query Should Look Like this. At Least in postgresql and MySql
$query = "INSERT INTO t_koperasi(id_koperasi,nama_koperasi,alamat,telp,hp,nama_cp,email,tanggal_fu,ket_fu,hasil_pembahasan,status) VALUES (
'".$id_koperasi."',
'".$nama_koperasi."',
'".$alamat."',
'".$telp."',
'".$hp."',
'".$nama_cp."',
'".$tanggal."',
'".$ket_fu."',
'".$hasil_pembahasan."',
'".$status."')"
;
You do not need to insert value into ID .Because ID expected to be the primary key with default value (auto increment).
Hope This works for you!
Simple remove id and ' ' . i hope work fine.
or
You just copy this code and past your project
<?php
include 'koneksi/koneksi.php';
if(isset($_POST['Submit'])) {
$id_koperasi = $_POST['id'];
$nama_koperasi = $_POST['nama'];
$alamat = $_POST['alamat'];
$telp = $_POST['telp'];
$hp = $_POST['hp'];
$nama_cp = $_POST['kontak'];
$email = $_POST['email'];
$nama_cp = $_POST['kontak'];
$tanggal = $_POST['tgl'];
$ket_fu = $_POST['ket'];
$hasil_pembahasan = $_POST['hasil'];
$status = $_POST['stat'];
$query = "INSERT INTO t_koperasi(id_koperasi,nama_koperasi,alamat,telp,hp,nama_cp,email,tanggal_fu,ket_fu,hasil_pembahasan,status) VALUES ('$id_koperasi',
'$nama_koperasi',
'$alamat',
'$telp',
'$hp',
'$nama_cp',
'$tanggal',
'$ket_fu',
'$hasil_pembahasan',
'$status')"
;
if (mysqli_query($con,$query)) {
header("location:index.php");
}else {
error_log( "This code has errors!" );
}
} include 'views/v_form.php';
I am new to mysql and php and i am self taught so there is no one to put me through. How can i remove this insertion process in my php file whenever i insert, because it is inserting well but i want to remove the process.
My PHP code is below
<?php
if(isset($_POST['insert_quiz'])) {
var_dump($_POST);
$quiz_add_id = $_SESSION['quiz_id'];
$count = $_SESSION['quiz_no'];
if (
!empty($_POST['question']) && !empty($_POST['optionA']) && !empty($_POST['optionB']) && !empty($_POST['optionC']) && !empty($_POST['optionD']) && count($_POST['op1']) === count($_POST['question']))
{
$question_array = $_POST['question'];
$optionA_array = $_POST['optionA'];
$optionB_array = $_POST['optionB'];
$optionC_array = $_POST['optionC'];
$optionD_array = $_POST['optionD'];
$op1_array = $_POST['op1'];
for ($i = 0; $i < $count; $i++) {
$question = $question_array[$i];
$op1 = $op1_array[$i];
$optionA = mysqli_real_escape_string($connection, $optionA_array[$i]);
$optionB = mysqli_real_escape_string($connection, $optionB_array[$i]);
$optionC = mysqli_real_escape_string($connection, $optionC_array[$i]);
$optionD = mysqli_real_escape_string($connection, $optionD_array[$i]);
$query = "INSERT INTO quizzes (quiz_add_id, quiz_question, quiz_option_A, quiz_option_B, quiz_option_C, quiz_option_D, quiz_option_correct)";
$query .= "VALUES ('{$quiz_add_id}', '{$question}', '{$optionA}','{$optionB}','{$optionC}','{$optionD}', '{$op1}')";
$create_quiz_query = mysqli_query($connection,$query);
}
if(!$create_quiz_query ) {
die('QUERY FAILED' . mysqli_error($connection));
}
//
}
}
?>
THis is what i dont want to see anymore when i insert
remove below code
var_dump($_POST);
I have this insert.php file that inserts data to my database but every time I submit data $result always returns false. I don't know where the problem is, its always echoing "something error".
##Insert.php
include_once("connection.php");
if (
isset($_POST['txtCarModel']) && isset($_POST['txtCarType']) &&
isset($_POST['txtCapacity']) && isset($_POST['image']) &&
isset($_POST['txtFuelType']) && isset($_POST['txtPlateNumber']) &&
isset($_POST['txtOwner'])
) {
$now = DateTime::createFromFormat('U.u', microtime(true));
$id = $now->format('YmdHis');
$upload_folder = "upload";
$path = "ftp://$upload_folder/$id.jpeg";
$image = $_POST['image'];
$fullpath = "carkila.esy.es/$upload_folder/$id.jpeg";
$Car_Model = $_POST['txtCarModel'];
$Car_Type = $_POST['txtCarType'];
$Capacity = $_POST['txtCapacity'];
$Fuel_Type = $_POST['txtFuelType'];
$PlateNumber = $_POST['txtPlateNumber'];
$Image = $_POST['image'];
$Owner = $_POST['txtOwner'];
$query = "INSERT INTO tbl_cars (Car_Model, owner, Car_Type,Capacity,fuelType,carPlatenuNumber,Image) VALUES ('$Car_Model', '$Owner', '$Car_Type',$Capacity,'$fuelType','$carPlatenuNumber','$Image')";
$result = mysqli_query($conn, $query);
if ($result > 0) {
if (isset($_POST['mobile']) && $_POST['mobile'] == "android") {
echo "success";
exit;
}
echo "Insert Successfully";
} else {
if (isset($_POST['mobile']) && $_POST['mobile'] == "android") {
echo "failed";
exit;
}
echo "Something Error";
}
$query = "INSERT INTO tbl_cars (Car_Model, owner, Car_Type,Capacity,fuelType,carPlatenuNumber,Image) VALUES ('$Car_Model', '$Owner', '$Car_Type',$Capacity,'$fuelType','$carPlatenuNumber','$Image')";
$carPlatenuNumber should be $plateNumber
Use mysqli_affected_rows ()
mysqli_query($conn, $query);
$result = mysqli_affected_rows ($conn);
It will return the number of rows affected by the previous operation.
mysqli_affected_rows
I am facing an error while inserting data records from CSV file to mysql database. I have a script which works fine. It inserts data from CSV file to mysql database. It has around 40,000 records. But after running the script, it inserts only around 1000 records and stops without error. What may be the problem? Please help.
Below is my script for inserting data.
include('dbconnection.php');
if (($handle = fopen("hotels.csv", "r")) !== FALSE) {
$flag = true;
$id=1;
while (($data = fgetcsv($handle, 100000000, ",")) !== FALSE) {
if($flag){
$flag = false;
continue;
}
$hotelid = mysql_real_escape_string($data[0]); //var_dump($hotelid); exit();
$hotelname = mysql_real_escape_string($data[1]);
$address1 = mysql_real_escape_string($data[2]);
$address2 = mysql_real_escape_string($data[3]);
$country = mysql_real_escape_string($data[4]);
$city = mysql_real_escape_string($data[5]);
$postcode = mysql_real_escape_string($data[6]);
$telephone = mysql_real_escape_string($data[7]);
$hotelfax = mysql_real_escape_string($data[8]);
$hotelemail = mysql_real_escape_string($data[9]);
$longitude = mysql_real_escape_string($data[10]);
$latitude = mysql_real_escape_string($data[11]);
$website = mysql_real_escape_string($data[12]);
$location = mysql_real_escape_string($data[13]);
$description = mysql_real_escape_string($data[14]);
$starrating = mysql_real_escape_string($data[15]);
$statecode = mysql_real_escape_string($data[16]);
$select_query = "SELECT * FROM `hotelDB`.`hotels` WHERE `hotelid` = $hotelid";
if(mysql_query($select_query) != '$hotelid'){
$sql = "INSERT IGNORE INTO `hotelDB`.`hotels` (`id`,`hotelid`, `hotelname`, `address1`,`address2`,`country`,`city`,`postcode`,`telephone`,`hotelfax`,`hotelemail`,`longitude`,`latitude`,`website`,`location`,`description`,`starrating`,`statecode`) VALUES ('$id','$hotelid', '$hotelname', '$address1','$address2', '$country', '$city', '$postcode', '$telephone', '$hotelfax', '$hotelemail', '$longitude', '$latitude', '$website', '$location', '$description', '$starrating', '$statecode')";
echo $sql;
$retval = mysql_query($sql,$conn);
if(! $retval )
{
die('Could not enter data: ' . mysql_error());
}
echo "<p style='color: green;'>Entered data having id = " .$id. " successfully</p><br>";
$id++;
}
}
echo "<br><p style='color: orange;'>Congratulation all data successfully inserted</p>";
fclose($handle);
}
mysql_close($conn);`
The insertion stops after inserting some values. This is example screenshot where the code stops executes.
You can try one query with multiple-row INSERT. In below example I am inserting 500 row with one insert query. You can change it to 1000.
<?php
include('dbconnection.php');
if (($handle = fopen("grnconnect-dump-hotels.csv", "r")) !== FALSE) {
//var_dump( $handle);
$flag = true;
$id=1;
$counter=0;
$sql = "INSERT IGNORE INTO `hotelDB`.`hotels` (`id`,`hotelid`, `hotelname`, `address1`,`address2`,`country`,`city`,`postcode`,`telephone`,`hotelfax`,`hotelemail`,`longitude`,`latitude`,`website`,`location`,`description`,`starrating`,`statecode`) VALUES";
$saveString="";
while (($data = fgetcsv($handle, 100000000, ",")) !== FALSE) {
//var_dump($data); exit();
if($flag){
$flag = false;
continue;
}
$hotelid = mysql_real_escape_string($data[0]); //var_dump($hotelid); exit();
$hotelname = mysql_real_escape_string($data[1]);
$address1 = mysql_real_escape_string($data[2]);
$address2 = mysql_real_escape_string($data[3]);
$country = mysql_real_escape_string($data[4]);
$city = mysql_real_escape_string($data[5]);
$postcode = mysql_real_escape_string($data[6]);
$telephone = mysql_real_escape_string($data[7]);
$hotelfax = mysql_real_escape_string($data[8]);
$hotelemail = mysql_real_escape_string($data[9]);
$longitude = mysql_real_escape_string($data[10]);
$latitude = mysql_real_escape_string($data[11]);
$website = mysql_real_escape_string($data[12]);
$location = mysql_real_escape_string($data[13]);
$description = mysql_real_escape_string($data[14]);
$starrating = mysql_real_escape_string($data[15]);
$statecode = mysql_real_escape_string($data[16]);
if(trim($counter)=="500") {
$sql = $sql.''.rtrim($saveString,",");
echo $sql;echo "<br /";
mysql_query($sql);
$counter=0;
$saveString="";
}
$saveString .="('".$id."','".$hotelid."', '".$hotelname."', '".$address1."','".$address2."', '".$country."', '".$city."', '".$postcode."', '".$telephone."', '".$hotelfax."', '".$hotelemail."', '".$longitude."', '".$latitude."', '".$website."', '".$location."', '".$description."', '".$starrating."', '".$statecode."'),";
$counter++;
}
if(trim($saveString)!=='') {
$sql = $sql.''.rtrim($saveString,",");
echo $sql;echo "<br /";
mysql_query($sql);
}
echo "<br><p style='color: orange;'>Congratulation all data successfully inserted</p>";
fclose($handle);
}
mysql_close($conn);
?>
Is there a particular reason to insert via script ?
Else I'd suggest you just insert manually or let the DB do it, sinces it's more efficient anyway. MySQL provides functions
for this: http://www.mysqltutorial.org/import-csv-file-mysql-table/
my php is returning null when values are passed to it i checked it with chrome's plugin POSTMAN... i am unable to figure out the problem :\
This is my code i am trying to update the database by sending values from my android code(which is okay):-
<?php
$response = array();
// check for required fields
if (isset($_POST['id']) && isset($_POST['company']) && isset($_POST['date']) &&
isset($_POST['time']) && isset($_POST['ten']) && isset($_POST['twelve']) &&
isset($_POST['aggregate']) && isset($_POST['backlog']) && isset($_POST['pending']) &&
isset($_POST['branch'])) {
$cid = $_POST['id'];
$company = $_POST['company'];
$date = $_POST['date'];
$time = $_POST['time'];
$ten = $_POST['ten'];
$twelve = $_POST['twelve'];
$aggregate = $_POST['aggregate'];
$backlog = $_POST['backlog'];
$pending = $_POST['pending'];
$branch = $_POST['branch'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql update row with matched pid
$result = mysql_query("UPDATE companies SET company = '$company', date = '$date', time
= '$time' ten = '$ten', twelve = '$twelve',aggregate = '$aggregate', backlog =
'$backlog', pending = '$pending', branch = '$branch' WHERE id = $cid");
// check if row inserted or not
if ($result) {
// successfully updated
$response["success"] = 1;
$response["message"] = "Company successfully updated.";
// echoing JSON response
echo json_encode($response);
} else {
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
You missed a comma:
= '$time', ten = '$ten', twelve = '$twelve', aggregate = '$aggregate', backlog =
____^_____