-1

私のtableXには、このようなデータがいくつかあります

<h1>ghhhhhh!</h1>
http://twitter.com/USERNAME
<h1></h1>
       http://3.bp.blogspot.com/_fqPQy3jcOwE/TJhikN8s5lI/AAAAAAAABL0/3Pbb3EAeo0k/s1600/Srishti+Rai1.html
<h1></h1>
http://4.bp.blogspot.com/_fqPQy3jcOwE/TJhiXGx1RII/AAAAAAAABLc/XNp_y51apks/s1600/anus7.html
<h1></h1>
http://cyz.com/_fqPQy3jcOwE/TJhh1ILX47I/AAAAAAAABKk/gX-OKEXtLFs/s1600/4r-2.html
<h1></h1>
http://cyz.com/_fqPQy3jcOwE/TJhiHGgb-KI/AAAAAAAABK8/zEv_41YzMhY/s1600/19+(1).html
<h1></h1>
http://cyz.com/_fqPQy3jcOwE/TJhihkpZZKI/AAAAAAAABLs/zDnlZkerBd8/s1600/Pooja+Gurung.html

同じphpコードをエコーすると正しい出力が得られますが、これらの詳細をmysqlに保存すると、mysql行に1行しか保存されません。私のコードはこれです

<?php 

include('connect.php'); 



 $idmg=$_POST["id"];
 $res =mysql_query("select * from tablea");
 $row = mysql_fetch_array($res);

if(sus== '0'){ 

  $output=''.$row['content'].''; 


 echo $output;//this output gives the above result but when i store in db it stores first row


mysql_query ("INSERT INTO tablea (content) VALUES ('$output')");


}    ?>
4

3 に答える 3

2

にエスケープされていないデータがあるため、挿入が失敗します$output。上記のDCoderのアドバイスに従い、PDOまたはmysqliを使用してください。

于 2012-08-15T19:16:13.363 に答える
2

mysql_fetch_array()ではなく sql_fetch_array() がある理由はあります か?

複数の行が必要な場合は、結果を反復処理する必要もあります。

<?php  
include('connect.php');   
$idmg =$_POST["id"]; 
$res  =mysql_query("select * from tablea"); 
$row  = sql_fetch_array($res)
  if($sus== '0'){  
   $output=mysql_real_escape_string($row['content']); 
   echo $output;
   mysql_query ("INSERT INTO tablea (content) VALUES ('$output')");  
}
?> 

@DCoder が言ったように、準備済みステートメントを使用する必要があります。現在のコードは SQL インジェクションに対して脆弱です。

于 2012-08-15T19:07:53.493 に答える
1

あなたのコードはいくらか修正されました:

<?php
    include('connect.php'); 

    $idmg=$_POST["id"];
    $res = mysql_query("select * from tablea");
    $row = mysql_fetch_array($res);

    if($sus== '0'){  // what is sus? If variable.. should be $sus
        $output = $row['content']; // .'' is literally nothing.. 
        echo $output;

        mysql_query ("INSERT INTO tablea (content) VALUES ('$output')");
    }
?>

あなたがやろうとしていると思うこと:

<?php
    include('connect.php'); 

    $idmg = $_POST["id"]; // not actually used
    $res = mysql_query('SELECT * FROM tablea');
    while($row = mysql_fetch_array($res)) {
        $output = $row['content'];
        echo $output;
        // do anything else you want.. in your case ?enter the data back in?
        mysql_query("INSERT INTO tablea(content) VALUES('$output')");
    }
?>

使用する必要があるもの:

<?php
    $idmg = $_POST['id']; // <-- not actually used
    $res = $mysqli_connection->query('SELECT * FROM tablea');
    while($row = $res->fetch_array(MYSQLI_ASSOC)) {
        $output = mysqli_connection->real_escape_string($row['content']);
        echo $output;
        // Do whatever else you like
        $mysqli_connection->query("INSERT INTO tablea(content) VALUES('$output')");
    }
    $res->free();
    $mysqli_connection->close();
?>
于 2012-08-15T19:14:10.830 に答える