I want to update the data with php - php

I want to update the data with PHP.when I click the button, the page refreshes and the update does not occur. How to correct
MY CODES:
File Name:Contact_settings
<form action="../netting/islem.php" method="post" class="form-horizontal form-label-left">
<div class="form-group">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Gsm Number<span class="required">*</span></label>
<div class="col-md-6 col-sm-6 col-xs-12">
<input type="text" name="ayar_tel" value="<?php echo $ayarcek['ayar_tel'] ?>" required="required" class="form-control col-md-7 col-xs-12">
</div>
<div class="form-group">
<div align="right" class="col-md-6 col-sm-6 col-xs-12 col-md-offset-3">
<button type="submit" name="iletisim" class="btn btn-success right">Güncelle</button>
</div>
</div>
FİLE NAME:islem.php
if(isset($_POST['iletisim'])){
$ayarkaydet = $db->prepare("UPDATE ayar SET
ayar_tel =:ayar_tel,
WHERE ayar_id=3
");
$update = $ayarkaydet->execute
(
array(
':ayar_tel' => $_POST['ayar_tel'],
)
);

you have an error in your code
must be like this
if(isset($_POST['iletisim'])){
$ayarkaydet = $db->prepare("UPDATE ayar SET ayar_tel =:ayar_tel WHERE ayar_id=3 ");
$update = $ayarkaydet->execute (array(':ayar_tel' => $_POST['ayar_tel']));
}
I delete two commas in your sql where and in your execute array and add a close } for the if sentence.

Related

Cannot bind date value using bindvalue function with PDO::PARAM_STR

So, I cannot find the solution to the problem I'm having. I'm really new to coding but learned how to start coding using basic HTML, PHP, PDO, and AJAX. So my problem comes from a form that retrieves dates from a calendar using the type=date from the form. The code of the form is down below.
<div class="col-lg-4">
<div class="panel panel-default">
<div class="panel-heading">
<h3 class="panel-title text-center"><i class="fa fa-bar-chart fa-fw"></i> Ingreso de reporte</h3>
</div>
<div id="alert_success" class="panel-body">
<br>
<form method="post" class="form-horizontal" role="form" action="ajax_form_post.php" id="insertreport">
<div class="form-group">
<label class="control-label col-sm-2" for="video" style="color:#777;">ID de video</label>
<div class="col-sm-10">
<input type="text" name="video" class="form-control" id="video" placeholder="Ingresa id del video" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="date_i" style="color:#777;">Fecha de arriendo</label>
<div class="col-sm-10">
<input type="date" name="date_i" class="form-control" id="date_i" placeholder="" required>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2" for="date_f" style="color:#777;">Fecha de devolución</label>
<div class="col-sm-10">
<input type="date" name="date_f" class="form-control" id="date_f" placeholder="" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-10">
<input type="hidden" name="c_id" class="form-control" id="user_id" value="<?php echo $id ?>" required>
</div>
</div>
<div class="form-group">
<div class="col-sm-offset-2 col-sm-10">
<input type="submit" class="btn btn-primary" name="update_customer" value="Enviar" id="submitdata">
</div>
</div>
</form>
<div class="text-right">
<i class="fa fa-arrow-circle-right"></i>
</div>
</div>
</div>
</div>
Now the problem starts with this Ajax form I built. BTW the script is working fine, the problem is inside this set of code.
<?php
/****************Get customer info to ajax *******************/
//require database class files
require("includes/pdocon.php");
//instatiating our database objects
$db = new Pdocon ;
if(isset($_POST['c_id'])){
$id = $_POST['c_id'];
$date_i = date("Y-m-d", strtotime($_POST['date_i']));
$date_f = date("Y-m-d", strtotime($_POST['date_f']));
$raw_v_id = clean_data($_POST['video']);
$v_id = val_int($raw_v_id);
$db->query('SELECT * FROM videos WHERE v_id = :v_id');
$db->bindvalue(':v_id', $v_id, PDO::PARAM_INT);
$row = $db->fetchSingle();
$db->query('INSERT INTO arriendo (transaccion, c_id, v_id, f_arriendo, f_devolucion)
VALUES (NULL, :c_id, :v_id :f_arriendo, :f_devolucion)');
$db->bindvalue(':f_arriendo', $date_i, PDO::PARAM_STR);
$db->bindvalue(':f_devolucion', $date_f, PDO::PARAM_STR);
$db->bindvalue(':c_id', $id, PDO::PARAM_INT);
$db->bindvalue(':v_id', $v_id, PDO::PARAM_INT);
$run = $db->execute();
}
if($run){
echo "<p class='bg-success text-center' style='font-weight:bold;'>Valor actualizado </p>";
}
?>
I get the following error:
Fatal error: Uncaught PDOException: SQLSTATE[42000]: Syntax error or access violation: 1064 You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near ''2021-08-05', '2021-08-06')' at line 2
Any help or a little guidance would be greatly appreciated. Thanks in advance.

Insert custom data from form to custom table on wordpress database

I wanna create some form for users to subscribe to our article and store the data in the database. I know that some plugin can do that but I don't know how to create the validation when the user already subscribed and the form never shows again.
How should I do to insert the data into my database?
Thank you!
the form
<form action="<?php echo site_url() . '/insert-data.php'; ?>" method="POST" name="form-subscribe">
<div class="row">
<div class="col-12 col-lg-12 col-md-12">
<p>Name*</p>
<input type="text" name="subs_name" placeholder="Full Name*" required="">
</div>
</div>
<div class="row mt-3">
<div class="col-12 col-lg-12 col-md-12">
<p>Email*</p>
<input type="text" name="subs_email" placeholder="Email Address*" required="">
</div>
</div>
<div class="row mt-3">
<div class="col-12 col-lg-12 col-md-12">
<p><button type="submit" class="btn btn-primary" name="submitForm">Submit</button></p>
</div>
</div>
</form>
the action file aka insert-data.php
<?php
//setting up the form
function insertuser() {
$name = $_POST['subs_name'];
$email = $_POST['subs_email'];
global $wpdb;
$table_name = $wpdb->prefix . "subscriber";
$wpdb->insert($table_name, array('subs_name' => $name, 'subs_email' => $email) );
}
if( isset($_POST['submitForm']) ) insertuser();
?>
Here is the table of wp_subscriber

saving bank row using array

I have a dynamic form which allows to add multiple textboxes. While saving the data in the database, it also saves an additional empty extra row. Could you help to find the issue?
HTML:
<div class="col-xs-12 col-sm-6">
<div class="form-group">
<input name="skill[]" type="text" class="form-control" placeholder="Skill name, e.g. HTML">
</div>
</div>
<div class="col-xs-12 col-sm-6">
<div class="form-group">
<div class="input-group">
<input name="percent[]" type="text" class="form-control" placeholder="Skill proficiency, e.g. 90">
<span class="input-group-addon">%</span>
</div>
</div>
</div>
<div class="col-xs-12 duplicateable-content">
<div class="item-block">
<div class="item-form">
<button class="btn btn-danger btn-float btn-remove"><i class="ti-close"></i></button>
<div class="row">
<div class="col-xs-12 col-sm-6">
<div class="form-group">
<input name="skill[]" type="text" class="form-control" placeholder="Skill name, e.g. HTML">
</div>
</div>
<div class="col-xs-12 col-sm-6">
<div class="form-group">
<div class="input-group">
<input name="percent[]" type="text" class="form-control" placeholder="Skill proficiency, e.g. 90">
<span class="input-group-addon">%</span>
</div>
</div>
</div>
</div>
</div>
</div>
Here is my php to save the data input:
for ($i = 0; $i < count($_POST["skill"]); $i++) {
$skill = $_POST["skill"][$i];
$percent = $_POST["percent"][$i];
$sql = "insert into tb_skill (skills,percent,user_id) values
('$skill', '$percent', '$_SESSION[id]')";
mysqli_query($con, $sql);
}
This might help to you.
$skill = $_POST['skill];
$percent = $_POST['percent'];
for ($i = 0; $i < = count($skill); $i++) {
$sql = "insert into tb_skill (skills,percent,user_id) values
('$skill[$i]', '$percent[$i]', '$_SESSION[id]')";
mysqli_query($con, $sql);
}
Add these lines inside of your for loop, before you submit the variables to database.
$skill = trim($skill);
$percent = trim($percent);

write data to mysql

Here is my problem I tray to write data to mysql but when I do input
and press submit button, got console log message from function wich
mean everythig is ok, but when I look to db have nothing to see. Can
anyone help me.
Second thing what I need to do is SELECT data from that db, then
that data + new data from input = data save to db.
here is html code :
<div class="body-content bg-1">
<div class="col-sm-12 col-xs-12" ng-controller="UnosUSkladisteCtrl">
<div class="container">
<div class="alert alert-info alert-dismissable"><strong>Info!</strong> {{data.message}}</div>
<div class="center">
<h1>Ulaz robe u skladište</h1>
</div>
<p ng-controller="LoginCtrl">Dobro došao <b>{{deName}}</b> | <a id="logout" href ng-click="logout()">Odjava</a></p>
</div>
<div class="nav-button center col-sm-4 col-xs-4">Povratak</div>
<div>
<form class="form-horizontal col-xs-12" col-sm-12" name="signUpForm" ng-submit="submitFormSignUp()" novalidate>
<!-- Zlatni medvjed -->
<div class="form-group" ng-class="">
<label class="col-sm-4 col-xs-12 control-label no-padding-right " for="zlatni_medvjed">Zlatni medvjed boca 0.5l</label>
<div class="col-sm-4 col-xs-12">
<span class="block input-icon input-icon-right">
<input ng-model="zlatni_medvjed" placeholder="Količina boca 0.5l" type="number" class="form-control">
</span>
</div>
</div>
<!-- Crna kraljica -->
<div class="form-group" ng-class="">
<label class="col-sm-4 col-xs-12 control-label no-padding-right " for="crna_kraljica">Crna kraljica boca 0.5l</label>
<div class="col-sm-4 col-xs-12">
<span class="block input-icon input-icon-right">
<input ng-model="crna_kraljica" placeholder="Količina boca 0.5l" type="number" class="form-control">
</span>
</div>
</div>
<!-- Grička vještica -->
<div class="form-group" ng-class="">
<label class="col-sm-4 col-xs-12 control-label no-padding-right " for="gricka_vjestica">Grička vještica boca 0.5l</label>
<div class="col-sm-4 col-xs-12">
<span class="block input-icon input-icon-right">
<input ng-model="gricka_vjestica" placeholder="Količina boca 0.5l" type="number" class="form-control">
</span>
</div>
</div>
<!-- Dva klasa -->
<div class="form-group" ng-class="">
<label class="col-sm-4 col-xs-12 control-label no-padding-right " for="dva_klasa">Dva klasa boca 0.5l</label>
<div class="col-sm-4 col-xs-12">
<span class="block input-icon input-icon-right">
<input ng-model="dva_klasa" placeholder="Količina boca 0.5l" type="number" class="form-control">
</span>
</div>
</div>
<!-- SUBMIT BUTTON -->
<label class="col-sm-4 control-label no-padding-right"></label>
<div class="col-sm-4">
<button ng-click="insertdata()" type="submit" class="btn btn-primary btn-lg btn-block">Unesi količine u skladište</button>
</div>
</form>
</div>
</div>
Here is js file code :
angular.module('angularLoginApp')
.controller('UnosUSkladisteCtrl', function($scope,$http) {
$scope.insertdata = function(){
$http.post("database/unos-piva.php", {'zlatni_medvjed':$scope.zlatni_medvjed, 'crna_kraljica':$scope.crna_kraljica, 'gricka_vjestica':$scope.gricka_vjestica, 'dva_klasa':$scope.dva_klasa })
.success(function(data,status,headers,config){
console.log("Podaci uspiješno spremljeni");
alert("Nove količine piva su dodane u skladište");
});
}
$scope.data = {message: "Molimo vas da točno navedete što unosite u skladište"};
});
and this is PHP file code to connect :
<?php
$data = json_decode(file_get_contents("php://input"));
$zlatni_medvjed = mysql_real_escape_string($data->zlatni_medvjed);
$crna_kraljica = mysql_real_escape_string($data->crna_kraljica);
$gricka_vjestica = mysql_real_escape_string($data->gricka_vjestica);
$dva_klasa = mysql_real_escape_string($data->dva_klasa);
mysql_connect("localhost","root","");
mysql_select_db("medvedgrad");
mysql_query("INSERT INTO stanje_piva(`zlatni_medvjed`, `crna_kraljica`, `gricka_vjestica`,`dva_klasa`)VALUES('"$zlatni_medvjed"','"$crna_kraljica"','"$gricka_vjestica"','"$dva_klasa"')")
?>
mysql columns
zlatni_medvjed, crna_kraljica, gricka_vjestica, dva_klasa
The format of the insert statement is wrong - you are incorrectly using quotes ( both single and double ) and the statement was not terminated with a semi-colon.
mysql_query("
INSERT INTO stanje_piva(`zlatni_medvjed`, `crna_kraljica`, `gricka_vjestica`,`dva_klasa`)
VALUES('{$zlatni_medvjed}','{$crna_kraljica}','{$gricka_vjestica}','{$dva_klasa}')
");
That said, this sql is vulnerable to sql injection and you are using the now deprecated mysql_* class - upgrade your code to mysqli or PDO and learn how to use Prepared Statements
As for the second question do an update stanje_piva ... set field=field+new data.... where id=1 etc ~ you would not need the initial select statement

If value already exists in array then issue an error in PHP

I have a code wherein it checks if a value already exists in the array. Basically, what the program does is that it first stores all the values in the array. Then it will be checked using count(array_keys) function. There are three inputs. If in those 3 inputs, a value occurs twice or thrice, it will issue an error. Now, my problem is that if INPUT A AND INPUT B IS THE SAME BUT INPUT C is different, it will still add to the database, BUT IF INPUT A AND C IS THE SAME BUT INPUT B is different then it will not add (which is correct).
Here is my php code:
<?php
include 'config.php';
if(isset($_POST['update_actual_lupong'])){
$id = isset($_GET['id'])? $_GET['id'] : "";
$id_hearing = $_POST['hearing_lup'];
$lupong = $_POST['act_lupong'];
$actual = array();
foreach($lupong as $aaa) {
$actual[] = $aaa;
}
if ((count(array_keys($actual, $aaa)) > 1)) {
echo '<br><br>this array contains DUPLICATE <br><br>';
}
else {
foreach ($lupong as $lup) {
$updateMemo = mysqli_query($conn, "INSERT INTO actuallupong(Hearing_idHearing, bar_pos2) VALUES ('$id_hearing', (SELECT idtable_position FROM table_position WHERE Person_idPerson = '$lup'));");
}
echo "ADDED ggg";
}
//header ('location: view_case_profile.php?id='.$id);
mysqli_close($conn);
}
?>
HTML code (it's in a modal):
<div class="modal fade bs-example-modal-lg" id="modal_lupong" data-backdrop="static" data-keyboard="false" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" >
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal">
<span aria-hidden="true">×</span>
<span class="sr-only">Close</span>
</button>
<h4 class="modal-title" id="myModalLabel">Update Lupong</h4>
</div>
<form id="update_actual_lupong" class="form-horizontal form-label-left calender" name = "update_actual_lupong" enctype="multipart/form-data" method="post" role="form" novalidate>
<div class="modal-body">
<div class="d item form-group">
<label class="col-sm-3 control-label">Hearing Number</label>
<div class="col-sm-7">
<input type="number" class="form-control" id="hearing_lup" name="hearing_lup" readonly="readonly"/>
</div>
</div>
<div class="f item form-group" id = "act1">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Lupong 1 <span class="required">*</span></label>
<div class="col-sm-7">
<input name="actlupong[]" id="search_keyword_id_act" class="search_keyword_act form-control col-md-7 col-xs-12" required="required" placeholder ="Search first name or last name... " type="text">
<input type="hidden" name="act_lupong[]" id="act_lup1"/>
<div id="result3"></div>
</div>
</div>
<div class="f item form-group" id = "act2">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Lupong 2 <span class="required">*</span></label>
<div class="col-sm-7">
<input name="actlupong[]" id="search_keyword_id_act1" class="search_keyword_act1 form-control col-md-7 col-xs-12" required="required" placeholder ="Search first name or last name... " type="text">
<input type="hidden" name="act_lupong[]" id="act_lup2"/>
<div id="result4"></div>
</div>
</div>
<div class="f item form-group" id = "act3">
<label class="control-label col-md-3 col-sm-3 col-xs-12">Lupong 3 <span class="required">*</span></label>
<div class="col-sm-7">
<input name="actlupong[]" id="search_keyword_id_act2" class="search_keyword_act2 form-control col-md-7 col-xs-12" required="required" placeholder ="Search first name or last name... " type="text">
<input type="hidden" name="act_lupong[]" id="act_lup3"/>
<div id="result5"></div>
</div>
</div>
</div>
<div class="modal-footer" style="margin:0;">
<button type="button" class="btn btn-default" data-dismiss="modal" style="margin-top: 4px;">Close</button>
<button id="send" type="submit" class="btn btn-success" name="update_actual_lupong">Save Record</button>
</div>
</form>
</div>
</div>
</div>
Screenshot:
What seems to be wrong in my code? Which part should I change? Your help will be much appreciated. Thank you.
As you said:- There are three inputs. If in those 3 inputs, a value occurs twice or thrice, it will issue an error.
A bit modification to your code needed:-
<?php
include 'config.php';
if(isset($_POST['update_actual_lupong'])){
$id = isset($_GET['id'])? $_GET['id'] : "";
$id_hearing = $_POST['hearing_lup'];
$lupong = $_POST['act_lupong'];
if (count(array_unique($lupong)) < count($lupong))) { // check that count of unique $lupong and original $lupong is equal or not if not then $lupong have duplicate values
echo '<br><br>this array contains DUPLICATE <br><br>';
}
else {
foreach ($lupong as $lup) {
$updateMemo = mysqli_query($conn, "INSERT INTO actuallupong(Hearing_idHearing, bar_pos2) VALUES ('$id_hearing', (SELECT idtable_position FROM table_position WHERE Person_idPerson = '$lup'));");
}
echo "ADDED ggg";
}
//header ('location: view_case_profile.php?id='.$id);
mysqli_close($conn);
}
?>
You exit the loop with $aaa as the value of c so only check this value for duplication.
You should check for duplicates inside the loop and set a variable i.e.
$dup = false;
foreach($lupong as $aaa) {
$actual[] = $aaa;
if ((count(array_keys($actual, $aaa)) > 1)) {
$dup = true;
}
}
if ($dup) {
echo '<br><br>this array contains DUPLICATE <br><br>';
}
else {
foreach ($lupong as $lup) {
$updateMemo = mysqli_query($conn, "INSERT INTO actuallupong(Hearing_idHearing, bar_pos2) VALUES ('$id_hearing', (SELECT idtable_position FROM table_position WHERE Person_idPerson = '$lup'));");
}
echo "ADDED ggg";
}

Categories