2013-10-28 31 views
-1
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,因此我希望它以ID 34,Lekan for Lastname,Balogun for Firstname和Grade8的身份進入課程。感謝預期。如何在發佈到mysql之前使用空格拆分字符串

+2

爆炸e()on「」字符 –

+0

您可以在空間上爆炸,但如果有任何空格不在字符串中,那麼它將不起作用。 –

回答

2

您可以使用explode() funnction在PHP這個

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

downvote Mr.Downvotter –

+0

的任何理由我認爲downvoter已經寫了他爲什麼在評論中這樣做的問題。 – TN888

+0

啊,再次沒有理由再投下一票。爲什麼你不說原因並將它降下來? –

2

您可以使用 「爆炸」 功能拆分字符串值,

$textArray = @explode(" ",$txt); 

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

這是你完整的代碼 如果(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); 
    } 
?> 
+0

我被告知避免使用'print',而是使用'echo'來提高速度。我可以避免使用打印,並且可以自動發佈爆炸數據嗎? Thanx –

+0

您可以使用var_dump而不是print_r來查看數據 – usii