0
if (mysqli_connect_errno()==0)
{ // if successfully connected

  $sent_dt=$_POST['scts'];
  // important: escape string values 
  $txt=mysqli_real_escape_string($con, $_POST['text']);
  $snd=mysqli_real_escape_string($con, $_POST['sender']);

  // creating an sql statement to insert the message into the SMS_IN table
  $sql="INSERT INTO SMS_IN(studentID,lastname,firstname,class) VALUES ('34','Lekan','Balogun','Grade8')";
  // executing the sql statement
  $insert_sms_success = mysqli_query($con,$sql);

  // closing the connection
  mysqli_close($con);
}

收到了一些不太清楚的信息。我希望能够使用中间的空格分隔传入的短信(例如 32 Lekan Balogun Grade8),将每个文本分隔为字段。我目前拥有的是整个短信。我希望能够使用文本之间的空格将其拆分到我的 MySql 数据库中。感谢期待,因为这是我的课堂作业

4

1 回答 1

0

You can use the Explode function in PHP to do this quite easily:

$yourOriginalText="Splitting posted text using space and inserting into mysql database";
$yourArrayofData=explode(" ", $yourOriginalText);
print_r($yourArrayofData);

You can then very easily work out how to insert the right data into the columns you want.

$sql="INSERT INTO SMS_IN(studentID,lastname,firstname,class) 
    VALUES 
    ('".$yourArrayofData[0]."',
    '".$yourArrayofData[1]."',
    '".$yourArrayofData[2]."',
    '".$yourArrayofData[3]."')";

Be aware though that this will simply take the data in the order it comes in - if someone mucks up an SMS, it will happily continue along. Also as you are passing user data into the database, you really want to do some injection protection.

于 2013-10-28T23:50:43.423 回答