|
 |
|
[PHP] ช่วยตรวจสอบ Code PHP+PDO INSERT ให้หน่อยค่ะไม่เข้าdatabase |
|
 |
|
|
 |
 |
|
หน้า action
<?php
session_start();
error_reporting(0);
include('includes/config.php');
if(isset($_POST['submit2']))
{
$FromDate=$_POST['FromDate2'];
$TimeFrom=$_POST['TimeFrom2'];
$Todate2=$_POST['tom'];
$TimeTo=$_POST['TimeTo2'];
$sl=$_POST['sl'];
$Place=$_POST['Place'];
$total=$_POST['total'];
$carin=$_POST['carin'];
$incar=$_POST['incar'];
$userEmail=$_SESSION['login'];
$status=0;
$sql2="INSERT INTO tblrent(userEmail,Vehicled,FromDate,TimeFrom,ToDate,TimeTo,sl,Place,total) VALUES('".$userEmail."','".$vhid."',
'".$FromDate."','".$TimeFrom."','".$Todate2."','".$TimeTo."','".$chsl."','".$Place."','".$total."')";
$query = $dbh->prepare($sql2);
$query->bindParam(':useremail',$useremail,PDO::PARAM_STR);
$query->bindParam(':vhid',$vhid,PDO::PARAM_STR);
$query->bindParam(':fromdate',$fromdate,PDO::PARAM_STR);
$query->bindParam(':TimeFrom',$TimeFrom,PDO::PARAM_STR);
$query->bindParam(':ToDate',$ToDate2,PDO::PARAM_STR);
$query->bindParam(':sl',$sl,PDO::PARAM_STR);
$query->bindParam(':Place',$Place,PDO::PARAM_STR);
$query->bindParam(':total',$total,PDO::PARAM_STR);
$query->execute();
$lastInsertId = $dbh->lastInsertId();
?>
หน้าฟอร์ม
<form action="confirm.php" method="POST">
<!-- <div class="form-group">
<input type="text" class="form-control" name="tell" placeholder="Phone" required>
</div> -->
<div class="form-group">
<!--<input type="selector" class="form-control" name="fromdate" placeholder="From Date(dd/mm/yyyy)" required> -->
<input type="hidden" class="form-control" name="id" value="<?php echo htmlentities($result->id);?>">
<input type="date" class="form-control" name="FromDate2" required>
<input type="time" class="form-control" name="TimeFrom2" id="myTime" >
</div>
<div class="form-group">
<!-- <input type="selector" class="form-control" name="todate" placeholder="To Date(dd/mm/yyyy)" required> -->
<input type="date" class="form-control" name="tom" required>
<input type="time" class="form-control" name="TimeTo2" id="myTime">
</div>
<input type="radio" name="sl" id="ckk1" onClick="ck_frm();" value="1" /><? if($sl=="รับรถเอง")?> รับรถเอง<br/>
<input type="radio" name="sl" id="ckk" onClick="ck_frm();" value="2"/><? if($sl=="ต้องการให้มาส่ง")?> ต้องการให้มาส่ง<br/>
<div id="frm_txt" style="display:none;">
<input class="form-control" type="text" name="Place" id="txtnum" value="" /><br/>
</div>
<?php if($_SESSION['login'])
{?>
<div class="form-group">
<input type="submit" class="btn" name="submit2" method="POST" value="เช่ารถยนต์">
</div>
<?php } else { ?>
<a href="#loginform" class="btn btn-xs uppercase" data-toggle="modal" data-dismiss="modal">เข้าสู่ระบบเพื่อเช่ารถ</a> <?php } ?>
</form>
<p><i>**ราคารวม <?php
echo htmlentities ($day);
?> บาท **</i></p>
<p><sup>ราคานี้รวมค่าประกันความเสียหายแล้ว</sup></p>
</form>
Tag : PHP, MySQL, HTML

|
|
 |
 |
 |
 |
Date :
2019-05-07 13:55:27 |
By :
289384415311490 |
View :
2622 |
Reply :
1 |
|
 |
 |
 |
 |
|
|
|
 |
 |
|
 |
 |
 |
|
|
 |
 |
|
วิธีการประกาศ statement ไม่ใช่แบบนี้นะครับ
$sql2="INSERT INTO tblrent(userEmail,Vehicled,FromDate,TimeFrom,ToDate,TimeTo,sl,Place,total) VALUES('".$userEmail."','".$vhid."',
'".$FromDate."','".$TimeFrom."','".$Todate2."','".$TimeTo."','".$chsl."','".$Place."','".$total."')";
สีแดงมันเอาค่ามาใส่แล้ว มันต้องทำเป็น ตัวแปร ตามรูปแบบ ถีงจะใช้ bindparam ได้
และการเขียนมันต้องเขียนให้แสดง error ออกมาให้ได้ เขียนแบบนี้เหมือนคนตาบอดคลำทาง
ตัวอย่างอย่างการใช้ตัวแปร และการแสดง error
Code (PHP)
$sql = 'insert into tablename (field1, field2, field3) values( :Fld1, :Fld2, :Fld3 )';
$stmt = $con->prepare($sql) or die( print_r($con->errorInfo(), true));
$stmt->bindParam(':Fld1',$value1,PDO::PARAM_STR);
$stmt->bindParam(':Fld2',$value2,PDO::PARAM_STR);
$stmt->bindParam(':Fld3',$value3,PDO::PARAM_STR);
$stmt->execute() or die( print_r($stmt->errorInfo(), true));
ปล. อ่าน document มันบ้างนะครับ แปลไม่ออกก็ให้ใช้ ลุงกูร์ฯ ดูบ้าง จะได้เข้าใจการทำงานของแต่ละคำสั่ง
|
ประวัติการแก้ไข 2019-05-07 14:42:17
 |
 |
 |
 |
Date :
2019-05-07 14:40:33 |
By :
Chaidhanan |
|
 |
 |
 |
 |
|
|
 |
 |
|
 |
 |
|
|
|

|
Load balance : Server 02
|