2016-10-06 5 views
0

PHPを使用してMySQLデータベースから単一のレコードを更新できるようにするために必要なページに取り組んでいます。誰か助けてくれますか? 1つのレコードを更新しようとすると、他のすべてのレコードが更新されます。私はPHPで1つのMySQL行を更新したいだけです

私の形である
<form action="" method="post"> 
    <input type="hidden" name="id" value="<?php echo $id; ?>"/> 
    <div> 
    <p><strong>ID:</strong> <?php echo $id; ?></p> 
    <strong>Name: *</strong> <input class="form-control" type="text" name="name" value="<?php echo $name; ?>" /><br/> 
    <strong>Month: *</strong> <input class="form-control" type="text" name="month" value="<?php echo $month; ?>" /><br/> 
    <strong>event1: *</strong> <input class="form-control" type="text" name="event1" value="<?php echo $event1; ?>" /><br/> 
    <strong>event2: </strong> <input class="form-control" type="text" name="event2" value="<?php echo $event2; ?>" /><br/> 
    <strong>event3: </strong> <input class="form-control" type="text" name="event3" value="<?php echo $event3; ?>" /><br/> 
    <strong>event4: </strong> <input class="form-control" type="text" name="event4" value="<?php echo $event4; ?>" /><br/> 
    <strong>timesub: </strong> <input class="form-control" type="text" name="timesub" value="<?php echo $timesub; ?>" readonly /><br/> 
    <p>* Required</p> 
    <input type="submit" name="submit" value="Submit" class="btn btn-info"> 
    <input type=reset name="reset" value="Reset" class="btn btn-danger"> 
    </div> 
    </form> 

、その後、私はこのコードでそれに従ってください:http://www.killersites.com/community/index.php?/topic/1969-basic-php-system-vieweditdeleteadd-records/

グレート記事:

<?php 
    } 
    include('db_connect.php'); 

    if (isset($_POST['submit'])) { 

    // confirm that the 'id' value is a valid integer before getting the form data 
    if (is_numeric($_POST['id'])) { 

    $id = $_POST['id']; 
    $name = mysql_real_escape_string(htmlspecialchars($_POST['name'])); 
    $month = mysql_real_escape_string(htmlspecialchars($_POST['month'])); 
    $event1 = mysql_real_escape_string(htmlspecialchars($_POST['event1'])); 
    $event2 = mysql_real_escape_string(htmlspecialchars($_POST['event2'])); 
    $event3 = mysql_real_escape_string(htmlspecialchars($_POST['event3'])); 
    $event4 = mysql_real_escape_string(htmlspecialchars($_POST['event4'])); 
    $timesub = mysql_real_escape_string(htmlspecialchars($_POST['timesub'])); 

    // check thatfields are filled in 
    if ($name == '' || $month == '' || $event1 == '' || $timesub == ''){ 

    // generate error message 
    $error = 'ERROR: Please fill in all required fields!'; 

    //error, display form 
    renderForm($id, $name, $month, $event1, $event2, $event3, $event4, $timesub, $error); 
    } else { 
    // save the data to the database 
    mysql_query("UPDATE announcement SET name='$name', month='$month', event1='$event1', event2='$event2', event3='$event3', event4='$event4', timesub='$timesub'") 
    or die(mysql_error()); 

    // once saved, redirect back to the view page 
    header("Location: view.php"); 
    } 
    } else { 
    // if the 'id' isn't valid, display an error 
    echo 'Error!'; 
    } 
    } else 
    // if the form hasn't been submitted, get the data from the db and display the form 
    {// get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0) 

    if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0) { 
    // query db 
    $id = $_GET['id']; 
    $result = mysql_query("SELECT * FROM announcement WHERE id=$id") 
    or die(mysql_error()); 

    $row = mysql_fetch_array($result); 

    // check that the 'id' matches up with a row in the databse 
    if($row) { 

    // get data from db 
    $name = $row['name']; 
    $month = $row['month']; 
    $event1 = $row['event1']; 
    $event2 = $row['event2']; 
    $event3 = $row['event3']; 
    $event4 = $row['event4']; 
    $timesub = $row['timesub']; 

    // show form 
    renderForm($id, $name, $month, $event1, $event2, $event3, $event4, $timesub, ''); 

    } else {// if no match, display result 

    echo "No results!"; 
    } 
    } else {// if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error 

    echo 'Error!'; 

    } 
    } 
    ?> 

私からのコードを持って!

ご協力いただきありがとうございます。

+2

基本的なSQL、具体的には[WHERE](https://en.wikipedia.org/wiki/Where_(SQL))節を習得する必要があります。また、[SQLインジェクション攻撃](http://bobby-tables.com)に脆弱であることに注意してください。 –

+2

あなたが 'WHERE id = $ id'のようなクエリでwhere節を追加する必要があるので。 mysql_ *は廃止され、PHP 7で廃止されていることにも注意してください。mysqli_ *またはPDOを使用してください – devpro

+0

Thanks Marc B;私は今SQLから始めていますが、SQLインジェクションに対する私の脆弱性はどこにありますか? – Samuel

答えて

3

まずはmysql_*の使用を中止してください。それはPHP 7で廃止され、閉鎖されています。mysqli_*またはPDOを使用できます。

いただきましたお問い合わせと間違っ:

、それはすべての行を更新しますよりも、あなたがUPDATE STATEMENTWHERE CLAUSEを使用しない場合、これは、非常に重要です。

あなたが何か、最後にあなたのクエリでWHERE CLAUSEを追加する必要がありますする必要があります。

WHERE id = '$id' 

はまた、あなたのコードは、SQLインジェクションのために開いている、ということに注意してください、あなたはあなたができる、SQLインジェクションを防ぐために必要がある必要があります準備された声明について学ぶ。この投稿は、あなたが理解するのに役立ちます:How can I prevent SQL injection in PHP?


*あなたは、この文書がお手伝いしますどのようにmysqli_*作品を知りたい場合は、次のhttp://php.net/manual/en/book.mysqli.php

あなたはPDOで仕事をしたい場合は、あなたをhttp://php.net/manual/en/book.pdo.php

+0

現在のコードをmysqli_ *に変更するには? – Samuel

+0

@SamFonseca:私が下に共有している参照リンクをchkしてください – devpro

+0

ありがとう、私はそれらをチェックしています...私はSQLに非常に新しいです。私は答えが受け入れられた場合、UPDATE、SELECT、INSERT INTOなどの基本コマンドを – Samuel

0

はあなたのクエリをよく見てください:

mysql_query("UPDATE announcement SET name='$name', month='$month', event1='$event1', event2='$event2', event3='$event3', event4='$event4', timesub='$timesub'") 

あなたのクエリがWHERE句が欠落しています。 WHERE句がないと、クエリはテーブルannouncementのすべての行を更新します。

WHERE id='$id' 

これにより、更新する行のみが更新されます。

+0

それを修正しました。感謝の男 – Samuel

+0

私は助けることができてうれしい。 –

関連する問題