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.
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
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.
i have form in which there are 2 dropdowns which are select batch from first dropdown and according to that batch, students will appear in second dropdown , Right now i am getting dropdown of batch but, not getting dropdown of students using batch
This is my view
<?php
use yii\helpers\Html;
use yii\widgets\ActiveForm;
use yii\helpers\ArrayHelper;
use common\models\TblBetch;
use common\models\TblCompany;
use common\models\TblCourse;
use common\models\TblStudent;
use common\models\TblStudentExpence;
use kartik\widgets\DepDrop;
use yii\db\Query;?><link rel="shortcut icon" href="<?php echo Yii::$app->params['global_theme_path'];?>images/logo1.png" type="image/png"><script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script><script src="http://iamrohit.in/lab/js/location.js"></script><div class="pageheader"><h2><i class="fa fa-money"></i>Expense</h2>
<div class="breadcrumb-wrapper">
<span class="label">You are here:</span>
<ol class="breadcrumb">
<li>Westline Shipping</li>
<li class="active">Add Expense</li>
</ol></div></div><div class="contentpanel">
<?php
$session = Yii::$app->session;
if($session->hasFlash('success'))
{
echo $session->getFlash('success');
}
if($session->hasFlash('error'))
{
echo $session->getFlash('error');
}
$site = '';
?>
<div class="row">
<div class="col-md-12">
<div class="container-fluid-50">
<div class="col-md-6">
<div class="panel panel-default">
<div class="panel-heading">
<div class="panel-btns">
−
</div>
<h4 class="panel-title">Add Expense</h4>
</div>
<form class="panel-body" action="dashboard" method="post">
<div class="row">
<?php $form = ActiveForm::begin(); ?>
<div class="col-sm-9">
<div class="form-group">
<label class="control-label"></label>
<?php
//$batch = new TblBetch();
//$quer = new Query();
//$getid = [''];
//$getid = $quer->select('b_sn')->from($batch->tableName())->all();
//echo $form->field($model,'se_b_id')->dropDownList($getid,['id'=>'se_b_id']);
?>
<?=
$form->field($model,'se_s_id')->dropDownList(
ArrayHelper::map(TblBetch::find()->all(),'b_id','b_sn'),
[
'prompt'=>'Select Batch',
'name'=>'s_b_id',
'id'=>'s_b_id',
'onchange'=>'$.post("student?id="+$(this).val(),function(data){$("select#tblstudentexpence-se_s_id").html(data);});'
]);
?>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label"></label>
<?=
$form->field($model,'se_s_id')->dropDownList(
ArrayHelper::map(TblStudent::find()->all(),'s_id','s_fname'),
[
'prompt'=>'Select Student',
'name'=>'se_s_id',
'id'=>'se_s_id'
]);
?>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Payment Mode</label>
<div class="radio"><label><input type="radio" name="num" value="cash" required> Cash </label></div>
<div class="radio"><label><input type="radio" name="num" value="check" required> Check </label></div>
<div class="radio"><label><input type="radio" name="num" value="dd" required> DD </label></div>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Check / DD No</label>
<input type="text" name="num" id="num" class="form-control" placeholder="Enter your check or DD number" required/>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group">
<label class="control-label">Amount</label>
<input type="text" name="lastname" class="form-control" placeholder="Enter your Amount" required/>
</div>
</div><!-- col-sm-6 -->
<div class="col-sm-9">
<div class="form-group"> <br><br>
<input type="submit" value="Submit" class="btn btn-primary">
<input type="reset" value="Cancel" class="btn btn-primary">
</div></div>
</div><!-- row -->
</form>
</div><!-- panel-body -->
</div>
</div>
</div> <div class="col-md-12">
</div>
</div></div></div> <script>
$(function() {
window.invalidate_input = function() {
if ($('input[name=num]:checked').val() == "check" || $('input[name=num]:checked').val() == "dd")
$('#num').removeAttr('disabled');
else
$('#num').attr('disabled', 'disabled');
};
$("input[name=num]").change(invalidate_input);
invalidate_input();
});
This is my controller (sitecontroller.php)
public function actionStudent($id)
{
//p($id);
$student = new TblStudent();
$queryobj = new Query();
$data = [];
$data = $queryobj->select('*')->from($student->tableName())->where(['s_betch'=>$id])->count();
//p($data);
$queryobj1 = new Query();
$getid = [];
$getid = $queryobj->select('*')->from($student->tableName())->where(['s_betch'=>$id])->all();
//p($getid);
if($data > 0)
{
foreach($getid as $gid)
{
echo "<option value='".$gid->s_id."'>".$gid->s_fname."</option>";
}
}
else
{
echo "<option> - </option>";
}
}
I am getting this response in firebug after ajax call
You can implement dependent drop-down much simpler in yii 2 because there is a widget available which you can use.
use kartik widget here -> kartik dependent drop down
I am creating a multiple choice quiz and I don't know how to insert multiple arrays to database. I can insert the first 2 arrays using array_combine but cannot on the 3rd, 4th, 5th, and 6th array. I can insert questions and answers but not the options A, B, C, and D.
I'm trying to do something like
foreach(array_combine($_POST['inQuestion'], $_POST['inAnswer'], $_POST['inA'], $_POST['inB'], $_POST['inC'], $_POST['inD']) as $question => $answer => $A => $B => $C => $D) {
Please help. Here is my code.
<?php
if(isset($_POST['btnCreate'])) {
$inQuestion = array($_POST['inQuestion']);
$inAnswer = array($_POST['inAnswer']);
$inA = array($_POST['inA']);
$inB = array($_POST['inB']);
$inC = array($_POST['inC']);
$inD = array($_POST['inD']);
$inLesson = $_POST['inLesson'];
$inQuizNo = $_POST['inQuizNo'];
$sql = "SELECT * FROM lessons WHERE title='$inLesson'";
$query = mysql_query ($sql);
$row = mysql_fetch_assoc($query);
$lessonID = $row['lessonID'];
foreach(array_combine($_POST['inQuestion'], $_POST['inAnswer']) as $question => $answer) {
$sql = "INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES ('$question', '$answer', '$A', '$B', '$C', '$D', '$lessonID', $inQuizNo)";
$query = mysql_query( $sql );
}
}
?>
<div class="panel panel-info">
<div class="panel-heading">
<h4>Create Assessment - Multiple Choice</h4>
</div>
<div class="panel-body">
<form method="post">
<br>
<div>
<div class="col-md-4">
<strong>Lesson</strong><br>
<select class="form-control" name="inLesson">
<option></option>
<option disabled></option>
<?php
$sql2 = "SELECT * FROM lessons WHERE courseID='$_GET[courseID]'";
$query2 = mysql_query ($sql2);
while ($row2 = mysql_fetch_assoc($query2)) {
?>
<option><?php echo $row2['title']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-4">
<strong>Quiz No.</strong><br>
<input required type="number" class="form-control" name="inQuizNo" min="1" max="100">
</div>
<div class="col-md-4">
<div class="pull-right">
<br>
<input type="button" value="Add" id="addButton" class="btn btn-info">
<input type="button" value="Remove" id="removeButton" class="btn btn-warning">
</div>
</div>
</div>
<br><br><br><br><br>
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1" class="form-group">
<h2><span class="label label-primary">No. 1</span></h2>
<br><br>
<div class="col-md-10">
<label>Question</label><input type='text' id='textbox1' name='inQuestion[]' class="form-control">
</div>
<div class="col-md-2">
<label>Answer</label><input type='text' id='textbox1' name='inAnswer[]' class="form-control">
<br>
</div>
<div class="col-md-3">
<label>A</label><input type='text' id='textbox1' name='inA[]' class="form-control">
</div>
<div class="col-md-3">
<label>B</label><input type='text' id='textbox1' name='inB[]' class="form-control">
</div>
<div class="col-md-3">
<label>C</label><input type='text' id='textbox1' name='inC[]' class="form-control">
</div>
<div class="col-md-3">
<label>D</label><input type='text' id='textbox1' name='inD[]' class="form-control">
<br><br>
</div>
</div>
</div>
<div class="col-md-12">
<button type="submit" name="btnCreate" class="btn btn-success pull-right">Create Quiz
</div>
</form>
</div>
From what I understood, you are trying to insert multible rows INTO your database.
First, when you use the [] in HTML forms you don't have to declare it as an array in php
Second, each question will have a question name, answer, A,B,C,D
When you trying to create the HTML form you could use this format to keep track the question_id or number.
<label>Question</label><input type='text' id='textbox1' name='inQuestion[1]' class="form-control">
Then you will be able to retrieve this item as $_POST['inQuestion'][1]
For Example, if you need 10 questions in your test page you can do it like this:
<?php for($i = 0; $i <= 10; $i++) : ?>
<form method="post">
<div class="col-md-10">
<label>Question</label><input type='text' id='textbox1' name='inQuestion[<?=$i;?>]' class="form-control">
</div>
<div class="col-md-2">
<label>Answer</label><input type='text' id='textbox1' name='inAnswer[<?=$i;?>]' class="form-control">
<br>
</div>
<div class="col-md-3">
<label>A</label><input type='text' id='textbox1' name='inA[<?=$i;?>]' class="form-control">
</div>
<div class="col-md-3">
<label>B</label><input type='text' id='textbox1' name='inB[<?=$i;?>]' class="form-control">
</div>
<div class="col-md-3">
<label>C</label><input type='text' id='textbox1' name='inC[<?=$i;?>]' class="form-control">
</div>
<div class="col-md-3">
<label>D</label><input type='text' id='textbox1' name='inD[<?=$i;?>]' class="form-control">
<br><br>
</div>
<input type="submit" value="go">
</form>
<?php endfor; ?>
Also, why do you put the content of the Answers A,B,C,D to an array while you have them is columns in your database. Also use these variables in your query too.
$inA = $_POST['inA'];
$inB = $_POST['inB'];
$inC = $_POST['inC'];
$inD = $_POST['inD'];
Finally Let's try to insert your values now.
if($_POST['btnCreate']){
// The form has been posted!
$inLesson = $_POST['inLesson'];
$inQuizNo = $_POST['inQuizNo'];
$questionsCount = count($_POST['inQuestion']);
$items = array();
// Since we should have the same size of arrays.
// Also the empty validation has been handled before this step!
for($i = 0; $i < $questionsCount; $i++){
$temp = array();
$temp['inQuestion'] = $_POST['inQuestion'][$i];
$temp['inAnswer'] = $_POST['inAnswer'][$i];
$temp['inA'] = $_POST['inA'][$i];
$temp['inB'] = $_POST['inB'][$i];
$temp['inC'] = $_POST['inC'][$i];
$temp['inD'] = $_POST['inD'][$i];
$items[] = $temp;
}
// Now items should have an array of all questions.
foreach($items as $item){
$SQL = 'INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES (
"'.$item['inQuestion'].'",
"'.$item['inAnswer'].'",
"'.$item['inA'].'",
"'.$item['inB'].'",
"'.$item['inC'].'",
"'.$item['inD'].'",
"'.$inLesson.'",
"'.$inQuizNo.'")';
$query = mysql_query( $SQL );
} // End Foreach
}
Try This
<?php
if(isset($_POST['btnCreate'])) {
$inQuestion = array($_POST['inQuestion']);
$inAnswer = array($_POST['inAnswer']);
$inA = $_POST['inA'] ? $_POST['inA'] : null;
$inB = $_POST['inB'] ? $_POST['inB'] : null;
$inC = $_POST['inC'] ? $_POST['inC'] : null;
$inD = $_POST['inD'] ? $_POST['inD'] : null;
$inLesson = $_POST['inLesson'];
$inQuizNo = $_POST['inQuizNo'];
$sql = "SELECT * FROM lessons WHERE title='$inLesson'";
$query = mysql_query ($sql);
$row = mysql_fetch_assoc($query);
$lessonID = $row['lessonID'];
$sql = "INSERT INTO `test` (question, answer, A, B, C, D, lessonID, quizNo) VALUES ('$question', '$answer', '$inA', '$inB', '$inC', '$inD', '$lessonID', $inQuizNo)";
$query = mysql_query( $sql );
}
?>
<div class="panel panel-info">
<div class="panel-heading">
<h4>Create Assessment - Multiple Choice</h4>
</div>
<div class="panel-body">
<form method="post">
<br>
<div>
<div class="col-md-4">
<strong>Lesson</strong><br>
<select class="form-control" name="inLesson">
<option></option>
<option disabled></option>
<?php
$sql2 = "SELECT * FROM lessons WHERE courseID='$_GET[courseID]'";
$query2 = mysql_query ($sql2);
while ($row2 = mysql_fetch_assoc($query2)) {
?>
<option><?php echo $row2['title']; ?></option>
<?php } ?>
</select>
</div>
<div class="col-md-4">
<strong>Quiz No.</strong><br>
<input required type="number" class="form-control" name="inQuizNo" min="1" max="100">
</div>
<div class="col-md-4">
<div class="pull-right">
<br>
<input type="button" value="Add" id="addButton" class="btn btn-info">
<input type="button" value="Remove" id="removeButton" class="btn btn-warning">
</div>
</div>
</div>
<br><br><br><br><br>
<div id='TextBoxesGroup'>
<div id="TextBoxDiv1" class="form-group">
<h2><span class="label label-primary">No. 1</span></h2>
<br><br>
<div class="col-md-10">
<label>Question</label><input type='text' id='textbox1' name='inQuestion[]' class="form-control">
</div>
<div class="col-md-2">
<label>Answer</label><input type='text' id='textbox1' name='inAnswer[]' class="form-control">
<br>
</div>
<div class="col-md-3">
<label>A</label><input type='text' id='textbox1' name='inA' class="form-control">
</div>
<div class="col-md-3">
<label>B</label><input type='text' id='textbox1' name='inB' class="form-control">
</div>
<div class="col-md-3">
<label>C</label><input type='text' id='textbox1' name='inC' class="form-control">
</div>
<div class="col-md-3">
<label>D</label><input type='text' id='textbox1' name='inD' class="form-control">
<br><br>
</div>
</div>
</div>
<div class="col-md-12">
<button type="submit" name="btnCreate" class="btn btn-success pull-right">Create Quiz
</div>
</form>
</div>
rather than posting a detailed solution, play with how to insert multiple rows to get some ideas
Something like:
$_POST['inQuestion'] = array(
'q1','q2','q3' // from input array name 'inQuestion[]'
);
$_POST['inAnswer'] = array(
'a1','a2','a3' // from input array name 'inAnswer[]'
);
$_POST['other'] = 'sunshine';
$_POST['yet_another'] = 'nice weather';
foreach($_POST['inQuestion'] as $k => $v){
$inserts[] = "(".$v.",".$_POST['inAnswer'][$k].",".$_POST['other'].",".$_POST['yet_another'].")";
}
echo "INSERT INTO table col_1, col_2, col_3, col_4 VALUES ".implode(',',$inserts)."";
// INSERT INTO table col_1, col_2, col_3, col_4 VALUES (q1,a1,sunshine,nice weather),(q2,a2,sunshine,nice weather),(q3,a3,sunshine,nice weather)