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 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?