猿问

我有一个带有输入作为图像的表单,我不知道如何将输入标签的name属性发送到mysql数据库?

因此,我希望用户从显示的表单中选择图像之一,但我不想上传图像,只需将该图像的名称发送到我的数据库即可。


我看到了一些有关将图像上传到数据库的解决方案,但是我不知道如何仅发送用户选择的图像的名称。


最终结果基本上是将信息发送到mysql数据库。


<p class="questions">What type of roof do you have?</p>

<div id="roof-type">

  <table>

    <tr>

      <form method="post" action="action.php">

        <input type="image" src="images/asphalt.png" name="asphalt" id="asphalt" />

        <input type="image" src="images/metal.png" name="metal" id="metal" />

        <input type="image" src="images/flat.png" name="flat" id="flat" />

        <input type="image" src="images/clay.png" name="clay" id="clay" />

        <input type="image" src="images/cement.png" name="cement" id="cement" />

        <input type="image" src="images/other.png" name="other-roof" id="other-roof" />

        <input type='hidden' name='roof_type' /> 


      </form>

    </tr>



    </table>

</div>

  




<script>

  let input = document.querySelector('input[type="hidden"][name="roof_type"]');

let col = document.querySelectorAll('input[type="image"]');

Array.prototype.slice.call(col).forEach(img => {

    img.addEventListener('click', function(e) {

      e.preventDefault();

      input.value = this.name;

      input.parentNode.submit();

    })

  })


  </script>

<?php

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

// Check connection

if ($conn->connect_error) {

    die("Connection failed: " . $conn->connect_error);

}



$roof_type = $_POST['roof_type'];


$sql = "INSERT INTO testing_database ". "(roof_type) ". "VALUES($roof_type)";


mysql_select_db('test');

        $retval = mysql_query( $sql, $conn );


        if(! $retval ) {

           die('Could not enter data: ' . mysql_error());

        }


        echo "Entered data successfully\n";


?>

我正在尝试检查是否回显了roof_type,但没有看到任何输出,但是此刻我什么都没有看到,这些是我得到的以下错误:


注意:未定义变量:C:\ xampp \ htdocs \ test \ action.php中的链接注意:未定义索引:C:\ xampp \ htdocs \ test \ action.php中的roof_type警告:mysqli_real_escape_string()期望参数1为mysqli, C:\ xampp \ htdocs \ test \ action.php中给出的null


翻翻过去那场雪
浏览 196回答 3
3回答

哈士奇WWW

既然您说jQuery是您的一个选择,那么...您可以不使用表单来执行此操作。例子<img src="images/flat.png" id="flat" class="imgSelect">然后在jQuery中$('.imgSelect').click(function(){&nbsp; var imgId = $(this).attr('id');&nbsp; $.ajax({&nbsp; method: "POST",&nbsp; url: "databaseProcess.php",&nbsp; data: { imgId: imgId }});您还可以向每个图像添加“ onclick”事件<img src="images/flat.png" id="flat" onclick="doFunction()" />
随时随地看视频慕课网APP
我要回答