dongpi0658 2016-02-19 10:41
浏览 53
已采纳

如何使用php回显从文本框表单发布的变量?

update : You can understand my problem if you see my test link. http://smstostudents.com/rmk/index3.php (select any name and submit)

Here is my HTML form code

<input type="checkbox" name="student[]" value="1"><label>student name 1/label>
<input type="checkbox" name="student[]" value="2"><label>student name 2</label>
<input type="checkbox" name="student[]" value="3"><label>student name 3</label>

<textarea id="message" class="input" name="message" rows="7" cols="50">
Dear $studentname you have not paid the fees, please pay it as soon as possible.
</textarea><br />

The Php code is as follows.

<?php
$message = $_POST['message'];
$servername = "localhost";
$dbusername = "xxxxxx";
$dbpassword = "xxxxxxx";
$dbname = "xxxxxxxx";
foreach($_POST['student'] as $value)
{
    // Create connection
    $conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    $getID = mysqli_fetch_assoc(mysqli_query($conn, "SELECT firstname, email FROM Students WHERE id = $value"));
    $studentname = $getID['firstname'];
    $email = $getID['email'];
    echo $message;
}
?>

What I am trying to achieve is, in the form text box "$studentname" will be replaced by student's name from the database. But echo $message doesn't display the students name, instead it just displays,

"Dear $studentname you have not paid the fees, please pay it as soon as possible."

how can I replace the "$studentname" by the Student's name. Sorry if it is easy, but I am stuck. Sorry, my English is bad, and I am a beginner in coding.

Update :

Actually i have no problem in the form. The form correctly posts the values to php. But, the textbox value contains "$studentname", which must be replaced with the student's name which is taken from the Database. But, the php code is not doing it, it is juts diplaying the textbox content as it is.

Thanks in advance.

  • 写回答

3条回答 默认 最新

  • dqyitt2954 2016-02-19 11:14
    关注

    First, you are connecting to the database inside a foreach loop... this is bad, and will make your script slower. Try moving the connection outside of the loop.

    Second, you are querying the database multiple times for data you want to gather from a single table. There is an easier way to do this.

    Here is an example:

    <?php
    
    
    $message = $_POST['message'];
    $servername = "localhost";
    $dbusername = "smstobdc_smstest";
    $dbpassword = "removable";
    $dbname = "smstobdc_smstest";
    
    // Create connection
    $conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    }
    //First we need to sanitize the values. or you will be open to a mysql injection...
    $students = array_map('mysql_real_escape_string',$_POST['students']); // this sanitizes all values inside $_POST['students'];
    //now let's prepare them for the query
    $students = "'".implode("','",$students)."'"; //this formats it like so: '1','2','3','4'....
    //now let's perform our query.. if you want to fetch all the results, then loop like this...
    //Make sure you perform the query and save the result resource so you can catch an error like so...
    $result = mysqli_query($conn, 'SELECT `firstname`, `email` FROM `Students` WHERE `id` IN ('.$students.')')
    //The actual query looks like this:
    //    SELECT `firstname`, `email` FROM `Students` WHERE `id` IN ('1','2','3','4','5','6'[,etc..])
    
    //now we can iterate through the results for each student. This while loop will continue until all the data specified by your student id's is returned
    while($getID = mysqli_fetch_assoc($result)) {
    
        $studentname = $getID['firstname'];
        $email = $getID['email'];   
    
        //NOTE: $studentname in $message is literally "$studentname". PHP will not treat this as a variable... what you can do is replace the text...
        echo str_replace('$studentname',$studentname,$message);
    }
    ?>
    

    If you just want the quick and dirty way to do it with your code then here is that as well:

    <?php
    
    
    $message = $_POST['message'];
    $servername = "localhost";
    $dbusername = "smstobdc_smstest";
    $dbpassword = "removable";
    $dbname = "smstobdc_smstest";
    
    
    
    foreach($_POST['student'] as $value) {
    
    // Create connection
    $conn = new mysqli($servername, $dbusername, $dbpassword, $dbname);
    // Check connection
    if ($conn->connect_error) {
        die("Connection failed: " . $conn->connect_error);
    } 
    
    $getID = mysqli_fetch_assoc(mysqli_query($conn, "SELECT firstname, email FROM Students WHERE id = $value"));
    $studentname = $getID['firstname'];
    $email = $getID['email'];   
    
        echo str_replace('$studentname',$studentname,$message);
    
    }
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 c语言写的8051单片机存储器mt29的模块程序
  • ¥60 求直线方程 使平面上n个点在直线同侧并且距离总和最小
  • ¥50 java算法,给定试题的难度数量(简单,普通,困难),和试题类型数量(单选,多选,判断),以及题库中各种类型的题有多少道,求能否随机抽题。
  • ¥50 rk3588板端推理
  • ¥250 opencv怎么去掉 数字0中间的斜杠。
  • ¥15 这种情况的伯德图和奈奎斯特曲线怎么分析?
  • ¥250 paddleocr带斜线的0很容易识别成9
  • ¥15 电子档案元素采集(tiff及PDF扫描图片)
  • ¥15 flink-sql-connector-rabbitmq使用
  • ¥15 zynq7015,PCIE读写延时偏大