如何在发布到mysql之前使用空格分割字符串

时间:2013-10-28 13:02:25

标签: php mysql

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);
}

拜托,我是php的新手,我从朋友那里得到了我的项目代码。我想在插入我的数据库之前使用空格(例如34 Lekan Balogun Grade8)拆分文本消息。我的表字段是:StudentID,LastName,FirstName,Class等我希望它发布为34作为ID,Lekan作为姓氏,Balogun作为名字,等级8进入课堂。感谢您的期待。

3 个答案:

答案 0 :(得分:2)

您可以在php中使用explode()功能

$str = '34 Lekan Balogun Grade8';
$arr = explode(' ',$str);
echo '<pre>';
print_r($arr);

答案 1 :(得分:2)

您可以使用“explode”功能

分割字符串值
 $textArray = @explode(" ",$txt);

  echo "<pre>";
  print_r($textArray);
  echo "</pre>";

答案 2 :(得分:1)

这是您完整的代码      if(mysqli_connect_errno()== 0)                 {//如果连接成功

              $sent_dt=$_POST['scts'];
              // important: escape string values 
              $txt=mysqli_real_escape_string($con, $_POST['text']);
              $snd=mysqli_real_escape_string($con, $_POST['sender']);
                $arr = explode(' ',$txt);
                echo '<pre>';
                print_r($arr);
                $StudentID = $arr['0'];
                $LastName  = $arr['1'];
                $FirstName = $arr['2'];
                $Class     = $arr['3']

              // creating an sql statement to insert the message into the SMS_IN table
              $sql="INSERT INTO SMS_IN(sms_text,sender_number,sent_dt) VALUES ('$txt','$snd','$sent_dt')";
              // executing the sql statement
              $insert_sms_success = mysqli_query($con,$sql);

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