【问题标题】:object of class mysqli _connect could not be converted to string in line 135mysqli _connect 类的对象无法在第 135 行转换为字符串
【发布时间】:2019-07-10 19:12:34
【问题描述】:

当我运行代码时,它在第 135 行给出了类 mysqli_connect 对象的错误无法转换为字符串。请有人为我提供此问题的解决方案。

我尝试寻找解决方案,但没有人为我工作。下面是显示错误的代码。

$row = mysqli_connect("$con,$insert_product");

接受运行但在第 135 行显示错误。我犯了什么错误。完整代码在这里

<?php require_once ("includes/db.php"); 
    ?>
    <!DOCTYPE html>
    <html>
    <head>
        <title> product</title>
    </head>
    <body>
    <form method="post" action="insert_product.php" enctype="multipart/form-data">
    <table width="700" align="center">
        <tr>
            <td><h2>insert new product</h2></td>
        </tr>
        <tr>
            <td>Product title</td>
            <td><input type="text" name="product_title"></td>
        </tr>
        <tr>
            <td>Product category</td>
            <td><select name="product_cat">
               <option>Select a category</option>
               <?php 
                  $get_cats = "select * from category";
                $run_cats= mysqli_query($con,$get_cats);
                while ($row_cats=mysqli_fetch_array($run_cats))
                {
                  $cat_id = $row_cats['cat_id'];
                    $cat_title=$row_cats['cat_title'];




                 echo "<option value='$cat_id'>$cat_title</option>";
                  }

                ?>
               </select>
            </td>
        </tr>
        <tr>
          <td>Product brand</td>
            <td><select name="product_brand">
               <option>Select brand</option>
               <?php 
                  $get_brand = "select * from brand";
                $run_brand= mysqli_query($con,$get_cats);
                while ($row_brand=mysqli_fetch_array($run_brand))
                {
                    $brand_id = $row_brand['brand_id'];
                    $brand_title=$row_brand['brand_title'];
                    echo "<option value='$cat_id'>$cat_title</option>";
                 }

                ?>
               </select>
            </td>
        </tr>
        <tr>
           <td>product image1</td>
            <td><input type="file" name="product_img1"></td>
        </tr>
        <tr>
           <td>Product image2</td>
            <td><input type="file" name="product_img2"></td>
        </tr>
        <tr>
            <td>Product image3</td>
            <td><input type="file" name="product_img3"></td>
        </tr>
        <
        <tr>
            <td>Product price</td>
            <td><input type="number" name="product_price"></td>
        </tr>
        <tr>
            <td>Product desceptration</td>
            <td><input type="text" name="product_desc"></td>
        </tr>

        <tr>
            <td>Product keyword</td>
            <td><input type="number" name="product_keyword"></td>
        </tr>
        <tr>
            <td><input type="submit" value="submit" name="submit"></td>
        </tr>

    </table>

    </form>
    </body>
    </html>


    <?php 

      if(isset($_POST['submit']))
      {
        $product_title=$_POST['product_title'];
        $product_cat=$_POST['product_cat'];
        $product_brand=$_POST['product_brand'];
        $product_price=$_POST['product_price'];
        $product_desc=$_POST['product_desc'];
        $status='on';
        $product_keyword=$_POST['product_keyword'];

        //image name

        $product_img1 = $_FILES['product_img1']['name'];
        $product_img2 = $_FILES['product_img2']['name'];
        $product_img3 = $_FILES['product_img3']['name'];

         //image temp names

        $temp_name1 = $_FILES['product_img1']['tmp_name'];
        $temp_name2 = $_FILES['product_img2']['tmp_name'];
        $temp_name3 = $_FILES['product_img3']['tmp_name'];


        if($product_title=='' OR  $product_cat=='' OR $product_brand=='' OR $product_price=='' OR $product_desc=='' OR $product_keyword=='' OR $product_img1=='' OR $product_img2=='' OR $product_img3=='')
          {
            echo "<script> alert ('please insert the data in the form')</script>";
            exit();
          }

        else{
            //uploadinimage to the folder
            move_uploaded_file($temp_name1,"product_images/$product_img1");
            move_uploaded_file($temp_name2, "product_images/$product_img2");
            move_uploaded_file($temp_name3,"product_images/$product_img3");

        }

        $insert_product = "insert into products (cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_brand,product_price,product_desc,status) values ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2',$product_img3,'product_brand','product_price','product_desc','product_keyword')";
      $row = mysqli_connect("$con,$insert_product");
        if($row)

        {
            echo "<script> alert('insert sucessfully')</script>";
        }
        else
        {
            echo "<script> alert(' unsucessfull to insert')</script>";
        }
    }
        ?>

【问题讨论】:

  • 其他查询是否有效?我看不到连接器的代码,我假设它包含在 db.php 中。
  • 你在 $row = mysqli_connect("$con,$insert_product"); 中有引号试试 $row = mysqli_query($con, $insert_product);
  • 而不是 $row = mysqli_connect("$con,$insert_product");使用 $row = mysqli_query($con, $insert_product);。您无需再次连接。只需要执行查询。还要删除使这些变量成为字符串的引号。

标签: php string class object mysqli


【解决方案1】:

我不认为你打算再次连接。相反,您似乎正在尝试执行查询?

您的代码:

$insert_product = "insert into products (cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_brand,product_price,product_desc,status) values ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2',$product_img3,'product_brand','product_price','product_desc','product_keyword')";
$row = mysqli_connect("$con,$insert_product");

您不仅引用了一个从未在脚本中定义的变量(尽管它可能在您需要的顶部文件中),而且您正在将查询权传递给 mysqli_connect()。我想你真正的意思是:

$insert_product = "insert into products (cat_id,brand_id,date,product_title,product_img1,product_img2,product_img3,product_brand,product_price,product_desc,status) values ('$product_cat','$product_brand',NOW(),'$product_title','$product_img1','$product_img2',$product_img3,'product_brand','product_price','product_desc','product_keyword')";
$row = mysqli_query($con, $insert_product);

【讨论】:

  • 这是固定的,但现在我有另一个错误。 mysqli_connect() 期望参数 1 是字符串。第 14 行 htdocs/shooping/admin-area insert_product.php 中给出的对象。你能帮帮我吗
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-05
  • 2015-07-27
  • 2012-04-29
  • 2011-04-06
相关资源
最近更新 更多