2013-10-28 22 views
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數據庫。由於在期待,因爲這是我的課堂作業使用空格分割文本並插入到mysql數據庫中

+0

https://www.google.co.nz/search?q=php+split+by+space&oq=php+split+by+space&aqs=chrome.0.57j60j65j60l3.5659&sourceid=chrome&ie=UTF-8 – jdog

回答

0

您可以使用PHP的爆炸功能來做到這一點很容易:

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

然後你可以很容易地制定出如何將正確的數據插入列你要。

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

要知道,雖然這會簡單地採取的順序談到數據 - 如果有人渣土了一條短信,它會很樂意繼續沿。同樣,當你將用戶數據傳遞到數據庫時,你確實想要做一些注入保護。

相關問題