它显示我的PHP编码的解析错误

时间:2018-05-30 06:20:30

标签: php

我正在尝试在Php中使用此代码

<?php
session_start();
$servername = "localhost";
$username = "root";
$password = "";
$dbname="fyp";
$num1 = $_POST['adultqty'];
$num2 = $_POST['childrenqty'];
$num3 = $_POST['scqty'];
$hint1 = $_POST['subtotal1'];
$hint2 = $_POST['subtotal2'];
$hint3 = $_POST['subtotal3'];
$total = $_POST['total'];
$user=3;
$cart_id=20;


if(isset($_POST['price1']))
    {




//Before we can access data in the MySQL database, we need to be able to connect to the server
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
    die("Connection failed: " . mysqli_connect_error());
}
        //$fname = $_GET['firstname'];
        //$lname = $_GET['lastname'];
        //echo $fname;
        //echo $lname;


        $sql = "INSERT INTO `cart` (`user_id`, `cart_id`, `total`, `gst`) VALUES ('$user', NULL, '$total', NULL);";
    $sql1 = "INSERT INTO `cart_details` (`cart_id`, `cart_detail_id`, `quantity`, `subtotal`) VALUES ('4', NULL, '$num1', '$hint1');";

    $sql2 = "INSERT INTO `cart_details` (`cart_id`, `cart_detail_id`, `quantity`, `subtotal`) VALUES ('4', NULL, '$num2', '$hint2');";

    $sql3 = "INSERT INTO `cart_details` (`cart_id`, `cart_detail_id`, `quantity`, `subtotal`) VALUES ('4', NULL, '$num3', '$hint3');";

        /*if(mysqli_query($conn, $sql)){
            echo"new record created succesfully";
        }
    if(mysqli_query($conn, $sql1)){
            echo"new record created succesfully";
        }
    if(mysqli_query($conn, $sql2)){
            echo"new record created succesfully";
        }
     if(mysqli_query($conn, $sql3)){
            echo"new record created succesfully";
        }
        else{
            echo "error" .$sql3."<br>". mysqli_error($conn);
        }*/


    }

  create PROCEDURE randomizer()
    begin
      declare random char(20) ;
      set random=conv(floor(rand() * 99999999999999), 20, 36) ;
      insert into `booking` (`user_id`, `booking_id`) VALUES (3,random) ;
  end;

/*echo "<br>".$num1."<br>".
 $num2."<br>".
 $num3."<br>".
 $hint1."<br>".
 $hint2."<br>".
 $hint3."<br>".
 $total."<br>";*/
include "checkout1.php";
?>
  

希望可以解决问题

我使用此代码,因为我想生成要插入我的数据库的随机字母数字字符串。我想创建使用随机函数的过程。但最后它出现了这个错误

我得到错误

  

解析错误:语法错误,意外情况&#39;程序&#39; (T_STRING)in   第65行的C:\ xampp \ htdocs \ checkout.php   第65行是:

create PROCEDURE randomizer()

1 个答案:

答案 0 :(得分:0)

通过查看代码,每次运行php脚本时,它都会尝试创建过程。即使它已经存在于mysql中。更好的方法是,您可以在mysql中创建过程,并从您的PHP代码中调用该过程。

你可以这样做: 在MySQL中执行此操作:

tx

然后你可以调用那个程序: 在PHP中执行此操作:

DELIMITER $$
CREATE DEFINER=`root`@`localhost` PROCEDURE `procedureName`(IN id int, IN booking_id Int // procedure parameters)
BEGIN
INSERT INTO database.bookings(`user_id`, `booking_id`) VALUES (id,booking_id)) // your actual SQL statement
END $$
DELIMITER ;