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';
Related
Kindly pardon me if i look silly mates.
My issue is , i have a profile page which on update and submit changes the data in db. To refresh the data then, i use select query after that. Then i save it in the current session. still the changes happens only in the page where the update and select query presents . I have some pages which is been included like top bar, left nav . which changes only if i refresh the page . My code is as follows
<?php
if(isset($_POST['update'])) {
$name_t = $_POST['name'];
$email_t = $_POST['email'];
$pass_t = $_POST['password'];
$contact_t = $_POST['contact'];
$address_t = $_POST['address'];
$dob_t = $_POST['dob'];
$religion_t = $_POST['religion'];
$pic_t = ($_FILES['imagefile']['name']);
$sql = "
UPDATE teacher
SET t_name = '$name_t'
, t_email = '$email_t'
, t_password = '$pass_t'
, t_phone = '$contact_t'
, t_address = '$address_t'
, t_dob = '$dob_t'
, t_religion = '$religion_t'
where teacher_id='$update_id'
";
$retval = mysql_query($sql,$link);
if(! $retval ) {
die('Could not update data: ' . mysql_error());
}
if (!empty($_FILES['imagefile']['name'])) {
$sql = "UPDATE teacher SET t_photo = '$pic_t' where teacher_id='$update_id' ";
$retval = mysql_query($sql,$link);
$info = pathinfo($_FILES['imagefile']['name']);
$ext = $info['extension']; // get the extension of the file
$target = 'img/upload/'.$pic_t;
move_uploaded_file( $_FILES['imagefile']['tmp_name'], $target);
}
}
$result = mysql_query("SELECT * FROM teacher where teacher_id='$update_id' ",$link);
while($row = mysql_fetch_array($result)){
$name = $row['t_name'];
$email = $row['t_email'];
$password = $row['t_password'];
$contact = $row['t_phone'];
$address = $row['t_address'];
$dob = $row['t_dob'];
$religion = $row['t_religion'];
$img = WEB_URL . 'img/upload/'.$row['t_photo'];
$_SESSION['objLogin'] = $row;
}
mysql_close($link);
?>
Kindly help me in updating the included page too without refresh . Because for instance there is the username displayed at top bar .
I am trying to set a session variable from page to another. It was working on my localhost but then I uploaded it to my server and now it fails. I have done some basic bug tests, and it will print at specific points, I have labeled them in the code. Here is the code the code I am using
//PAGE 1
session_start();
if(isset($_REQUEST['id'])){
$_SESSION['id'] = $_REQUEST['id'];
//prints here just fine then fails after
header('location: app.php');
}elseif($_POST){
if(isset($_SESSION['error'])){
unset($_SESSION['error']);
}
$questions = array();
for($i = 1; $i <= 11; $i++){
$questions['q'.$i] = $_POST['q'.$i];
}
$check = "";
foreach($questions as $key => $val){
$check .= $val;
}
$metreq = strcmp($check, "1111-1-1-1-11-1-1");
if(!$metreq){
$_SESSION['questionaire'] = $questions;
header("location: app.php");
}else{
header('location: nq.php');
}
}
///PAGE 2
<?php
error_reporting(E_ALL); ini_set('display_errors', 1);
session_start();
require_once('inc/state_list_options.php');
if(isset($_SESSION['questionaire']) || isset($_SESSION['id'])){
$id = $first_name = $middle_name = $last_name = $suffix = $address = $city = $state = $zip = $phone_area = $phone_exch =
$phone_num = $phone2_area = $phone2_exch = $phone2_num = $email_address = $birth_month = $birth_day = $birth_year =
$job = $referred = "";
$selected = "selected";
$questions = array();
//here we will generate a session id that is the code passed from the link clicked by the user
$conn = mysql_connect('host', 'user', 'password');
mysql_select_db('db', $conn);
if($conn){
if(isset($_SESSION['id'])){
$id = $_SESSION['id'];
$q = "SELECT * FROM eagle.pre_application WHERE id = '$id'";
$r = mysql_query($q, $conn);
$row = mysql_fetch_assoc($r);
$first_name = $row['first_name'];
$middle_name = $row['middle_name'];
$last_name = $row['last_name'];
$suffix = $row['suffix'];
$address = $row['address'];
$city = $row['city'];
$state = $row['state'];
$zip = $row['zip'];
$phone_area = $row['phone_area'];
$phone_exch = $row['phone_exch'];
$phone_num = $row['phone_num'];
$phone2_area = $row['phone2_area'];
$phone2_exch = $row['phone2_exch'];
$phone2_num = $row['phone2_num'];
$email_address = $row['email_address'];
$birth_month = $row['birth_month'];
$birth_day = $row['birth_day'];
$birth_year = $row['birth_year'];
$questions['q1'] = $row['q1'];
$questions['q2'] = $row['q2'];
$questions['q3'] = $row['q3'];
$questions['q4'] = $row['q4'];
$questions['q5'] = $row['q5'];
$questions['q6'] = $row['q6'];
$questions['q7'] = $row['q7'];
$questions['q8'] = $row['q8'];
$questions['q9'] = $row['q9'];
$questions['q10'] = $row['q10'];
$questions['q11'] = $row['q11'];
$_SESSION['questionaire'] = $questions;
}
$q2 = "SELECT job_posting.id, job_posting.title, location.city, location.state
FROM eagle.job_posting
LEFT JOIN eagle.location
ON job_posting.location__Id = location.id";
$j = mysql_query($q2, $conn);
?>
<!-- job application goes ehre -->
<?
}else{
$_SESSION['error'] = "Please fill out the questionaire before completing the applicationn.";
header('location: index.php');
exit;
}
}else{
echo "There is an error";
}
?>
I'm not 100% sure where to go from this point forward. I was thinking that it was the headers causing and issue, but that has never appeared so at this poitn I am baffled. Please let me know what you think about this. If you need additional code or further examples please let me know and I can provide them for you. Thank you in advance to anyone who stops by to help!
EDITS/DEVELOPMENTS/UPDATES
9:07 AM: Using developer's tools on chrome it is now receiving a 500 server error when submitting to page 2
9:16 AM Removed errors found with the require_once(''); There was an error in the path. Now it just automatically goes to the redirect on page 2 even if a variable is manually passed in the header via the address bar. I'm stumped still. code updated to reflect changes
At the moment this code updates the user info in MYSQLTABLE1. What I also want done is user info to be copied to WMYSQLTABLE which I can do but I also want a code from MYSQLTABLE2 to be copied over into a column in WMYSQLTABLE as well. Here is the part I need changing:
$sql_insert2 = "INSERT INTO ".WMYSQLTABLE2."(ip,date,address)values
('','$ip','$date','$address')";
$res_insert2 = mysql_query($sql_insert2)or die(mysql_error());
//Need to also insert code from first row from column named 'codes' in MYSQLTABLE2.
Actual code, it's a bit messy at the moment and the if/else statements do the exact same at the moment. It works but I get the error " Column count doesn't match value count at row 1" because I cannot fill the last column with the code from MYSQLTABLE2.
<?php
include("includes/config.php");
include("includes/mysql.php");
include("amount.php");
if(isset($_POST['func']))
{
$address = $_POST['address'];
$ip = $_POST['ip'];
$date = $_POST['date'];
$time = $_POST['time'];
$price = $_POST['price'];
$increment = $_POST['increment'];
$id = $_POST['id'];
$num = 0;
$sql_check_address = "SELECT * FROM ".MYSQLTABLE1." WHERE address='$address'";
$res_check_address = mysql_query($sql_check_address)or die(mysql_error());
$num = mysql_num_rows($res_check_address);
$row = mysql_fetch_assoc($res_check_address);
if($num > 0)
{
$address = $row['address'];
$ip = $row['ip'];
$date = $row['date'];
$oldprice = $row['price'];
$id = $row['id'];
$newprice = $oldprice - $payAmount*200;
$newinc = $increment - 200;
$sql_update1 = "UPDATE ".MYSQLTABLE1." SET ip='$ip',date='$date',price='$newprice',increment='$newinc',address='$address' WHERE id='$id'";
$res_update1 = mysql_query($sql_update1)or die(mysql_error());
/////////////////Insert user info and copy code from MYSQLTABLE2 to WMYSQLTABLE2
$sql_insert2 = "INSERT INTO ".WMYSQLTABLE2."(ip,date,address)values
('','$ip','$date','$address')";
$res_insert2 = mysql_query($sql_insert2)or die(mysql_error());
}
else{
$address = $row['address'];
$ip = $row['ip'];
$date = $row['date'];
$oldprice = $row['price'];
$id = $row['id'];
$newprice = $oldprice - $payAmount*200;
$newinc = $increment - 200;
$sql_update = "UPDATE ".MYSQLTABLE1." SET ip='$ip',date='$date',price='$newprice',increment='$newinc',address='$address' WHERE id='$id'";
$res_update = mysql_query($sql_update)or die(mysql_error());
/////////////////Insert user info and copy code from MYSQLTABLE2 to WMYSQLTABLE2
$sql_insert2 = "INSERT INTO ".WMYSQLTABLE2."(ip,date,address)values
('','$ip','$date','$address')";
$res_insert2 = mysql_query($sql_insert2)or die(mysql_error());
e
}
}
Any help will be greatly appreciated.
The general form of the query would be:
$sql_insert = "INSERT INTO " . WMYSQLTABLE2 . "(code, ip, date, address)
SELECT code, '$ip', '$date', '$address'
FROM OtherTable
WHERE <put something here to select the row>";
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'
I am trying to edit this code so that instead of just inserting the info - it checks to see if the file already exists in the database, if it does it inserts as it does now. If it does exist... it should just update the info, adding the "value" amount to the value amount already in there instead of replacing it. But this is very new to me and I am lost so any help with really be appreciated!
<?php
define('JPATH_BASE',$_SERVER['DOCUMENT_ROOT']);
require_once($_SERVER['DOCUMENT_ROOT']."/b2/configuration.php");
require_once($_SERVER['DOCUMENT_ROOT']."/b2/libraries/joomla/factory.php");
require_once($_SERVER['DOCUMENT_ROOT']."/b2/libraries/joomla/base/object.php");
require_once($_SERVER['DOCUMENT_ROOT']."/b2/libraries/joomla/database/database.php");
require_once($_SERVER['DOCUMENT_ROOT']."/b2/libraries/joomla/database/database/mysql.php");
$config = new JConfig;
$options = array();
$options['host'] = $config->host;
$options['user'] = $config->user;
$options['password'] = $config->password;
$options['database'] = $config->db;
$options['prefix'] = $config->dbprefix;
$options['select'] = true;
$db = new JDatabaseMySQL($options);
//$user = JFactory::getUser();
$userId = 0;
//($user->id > 0) ? $user->id : 0;
$numbervote = $_REQUEST['w'];
$folder = $_REQUEST['w2'];
$name = $_REQUEST['w1'];
$date = date('Y-m-d H-i-s');
$sql = "INSERT INTO jos_image_ratetting (id,userid,datecreated,value,filename,folder,md5name) VALUES(NULL,'".$userId."','".$date."','".$numbervote."','".$name."','".$folder."',MD5('".$name."'))";
$db->setQuery($sql);
if($db->query()){
$msg = "Thanks for rate!";
}else{
$msg = mysql_error();
}
echo $msg;
//echo 'Hello';
?>
Take a look at the ON DUPLICATE KEY UPDATE syntax for MySQL