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条)

报告相同问题?

悬赏问题

  • ¥15 怎么实现不管输入多少字符或者数组或者空格 输出的时候先显示输入的字符数组或者空格再提示输出。输出的结果每个字符后面都带上一个空格
  • ¥15 ros小车启动launch文件报错
  • ¥15 vs2015到期想登陆但是登陆不上
  • ¥15 求解四个变换矩阵中的未知数θ
  • ¥20 Intellij实现web登录界面
  • ¥15 IPQ5018制作烧录固件,boot运行失败(操作系统-linux)(相关搜索:操作系统)(相关搜索:操作系统)
  • ¥20 icefall在librispeech基础上加入个人数据集
  • ¥30 keepalive高可用故障运维配置询问
  • ¥15 求帮助!国家电网内网u盘突然识别不出来了。
  • ¥15 matlab语音变速变调同时实现