duanliang5051 2017-02-28 14:32
浏览 28
已采纳

您可以在插入数据时对变量求和或添加吗?

I would like to ask if it is possible to add together with a variable, I've manage it to echo it and it worked.

But is it possible in a insert statement?

Here is my code!

<?php 
include('admin/db/database_configuration.php');
$sql = "SELECT * FROM tblalbums";
   $result = $conn->query($sql);

    if ($result->num_rows > 0) {
        // output data of each row
       $row = $result->fetch_assoc();
       $wow = $row['album_id'];
    echo ($row['album_id']+1);
} else {
    echo "0 results";
}
$conn->close();
?>

<?php
include('admin/db/database_configuration.php');
if (isset($_FILES["userfile"]) && !empty($_FILES["userfile"])) {
    $image = $_FILES['userfile']['tmp_name'];

    $imageSize = $_FILES['userfile']['size'];
    $imageType = $_FILES['userfile']['type'];
    // $job_desc = $_POST['desc'];
    $imageName = $_FILES['userfile']['name'];

    $len = count($image);
    $path = "jobs/";
    for ($i = 0; $i < $len; $i++) {
         if (isset($imageName[$i]) && $imageName[$i] !== NULL) {
             if(move_uploaded_file($image[$i], $path.$imageName[$i])) {
                // $result = $conn->query("INSERT INTO tbljoba ( job_img_size, job_img_type, job_desc, job_img) VALUES ('$imageSize[$i]', '$imageType[$i]', '$job_desc', '$imageName[$i]')");
                $sql = "INSERT INTO `tblphotos` ( imageName, imageSize, imageType, album_id) VALUES ('$imageName[$i]','$imageSize[$i]', '$imageType[$i]', '$wow');";
                // echo"<script>alert('Image upload successfully!');location.href='employer_contact_us.php';</script>";

                 if ($conn->query($sql) === TRUE) {
                    echo "New records created successfully";
                } else {
                    echo "Error: " . $sql . "<br>" . $conn->error;
                }
            }
         }
    }
}
$conn->close();

?>

As you can see above the variable $wow is album_id in the database, and i've echo it so the last number in the database + 1.

So i've got the INSERT statement and you can see there I've put the variable $wow. So how can add or sum up + 1 in adding values?

Thank you!

  • 写回答

2条回答 默认 最新

  • dongshan4518 2017-02-28 14:46
    关注

    remove single quoted lines ' ' from $wow in VALUES then +1 $wow=1 without single quoted lines so that it will not read as a string to the database

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私