douyi3833 2019-05-07 20:54
浏览 55

一次提交多个sql语句

I am trying to have multiple SQL queries fire off on one summit, One that updates a form and one that updates a space in the db for a file ext so that image can be echoed out. I had it fire on a separate "post" BTN and that worked but, now that I am trying to ""combined queries I get undefined index for the info for my file. So the overall FM of this problem is to update all these records and then once that this done it will move a copy of the uploaded image to a folder and then foreach "customer " there will be a space in the db for a specific image to be assigned to them.

<?php

    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "techfusioncustomers";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Get the customer ID.
    $customerId = intval($_GET['id']);

    // If form was submitted, update the customer.
    if (!empty($_POST['customerID'])) {

        $customerUpdated = true;

        $sql = "UPDATE customers
            SET
                address = ?,
                bPhone = ?,
                city = ?,
                companyName = ?,
                cPhone = ?,
                customerStatus = ?,
                email = ?,
                faxNumber = ?,
                firstName = ?,
                gradDate = ?,
                howDidYouHear = ?,
                hPhone = ?,
                isCustTaxExempt = ?,
                lastName = ?,
                leadRating = ?,
                leadServiceIntrest = ?,
                leadSource = ?,
                leadStatus = ?,
                Ext = ?,
                state = ?,
                studentStatus = ?,
                zip = ?
            WHERE customerID = ?";

        $stmt = $conn->prepare($sql);

        if ($stmt === false) {
            die($conn->error);
        }

        $stmt->bind_param(
            'ssssssssssssssssssssssi',
            $_POST['address'],
            $_POST['bPhone'],
            $_POST['city'],
            $_POST['companyName'],
            $_POST['cPhone'],
            $_POST['customerStatus'],
            $_POST['email'],
            $_POST['faxNumber'],
            $_POST['firstName'],
            date("Y-m-d", strtotime($_POST['gradDate'])),
            $_POST['howDidYouHear'],
            $_POST['hPhone'],
            $_POST['isCustTaxExempt'],
            $_POST['lastName'],
            $_POST['leadRating'],
            $_POST['leadServiceIntrest'],
            $_POST['leadSource'],
            $_POST['leadStatus'],
            $_POST['Ext'],
            $_POST['state'],
            $_POST['studentStatus'],
            $_POST['zip'],
            $_POST['customerID']
        );

        $stmt->execute();

        if ($stmt->error) {
            die($stmt->error);
        }

        $stmt->close();
    }

        /

    / Load the customer.
            $result = $conn->query("SELECT * FROM customers WHERE customerID = {$customerId}") or die($conn->error);
            $customer = $result->fetch_assoc();

            $conn->close();



    ?>

I have this and want to combined it with this

$servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "techfusioncustomers";

    // Create connection
    $conn = new mysqli($servername, $username, $password, $dbname);

    $msg = "";
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }

    // Get the customer ID.
    $customerId = $_POST['customerId'];
    $image = $_FILES['image']['name'];
    $image_text = mysqli_real_escape_string($conn, $_POST['image_text']);
    $target = "images/" . basename($image);

    // If form was submitted, update the customer.
    if (isset($_POST['upload'])) {

        $sql = "UPDATE customers set image = '$image', image_text = 
        '$image_text' 
                 where customerID = '$customerId'";

        mysqli_query($conn, $sql) or die(mysqli_error($conn));

        $query = mysqli_query($conn, $sql);

        if (!$query){
            exit("file could not be moved");
        }

        if (move_uploaded_file($_FILES['image']['tmp_name'], $target)) {
          echo  $msg = "Image uploaded successfully";
        } else {
           echo $msg = "Failed to upload image";
        }
    }

    // Load the customer.
    $result = $conn->query("SELECT * FROM customers WHERE customerID = 
    {$customerId}") or die($conn->error);
    $customer = $result->fetch_assoc();

    $conn->close();

I get undefined index for image. I want to have this all go when if (!empty($_POST['customerID'])) because as of right now that image uploader sits in its own form and has its own "submit" it waits for.So I want to have the image uploader

<form method="POST" action="test.php" enctype="multipart/form-data">
            <input type='hidden' name='customerId' value='<?= $customerId 
             ?>'>

            <div>
                <input type="file" name="image">
            </div>
            <div>
                <textarea id="text" cols="40" rows="4" name="image_text" placeholder="Say something about this image..."></textarea>
            </div>
            <div>
                <button type="submit" name="upload">POST</button>
            </div>

        </form>

This above is what the uploader looks like I know I would have to get rid of the action and (All the php sits at the top of the html form where it garbs all the rest of the data)

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条
    • ¥15 关于#windows#的问题:怎么用WIN 11系统的电脑 克隆WIN NT3.51-4.0系统的硬盘