samedi 26 mars 2016

Fetch data from database from msql_ to PDO

I'm very new to PDO and I want this mysql_ to be replaced in PDO but I have no idea how. Help?

This is the link where I click EDIT

<a href="/sec/editinfo.php?edit_id=<?php echo $row[0]; ?>">EDIT</a>

This is my editinfo.php where it will go after you click the EDIT link

<?php 
if(isset($_GET['edit_id']))
    {

    $sql = "SELECT * FROM news WHERE id=".$_GET['edit_id']; 
    $result = mysql_query($sql);
    $row = mysql_fetch_array($result);
?>
<form action="/sec/updatepost.php" method="post">
<div>
<center><input type="hidden" name="id" value="<?php echo  $row['id'] ?>"/></center>
    <div class="control-group form-group">
        <div class="controls">  
            <label> Title </label> 
                <input type="text" name="title" class="form-control" size="103" maxlength="50" value="<?php echo $row['title']; ?> "/>
                    <p class="help-block"></p>
        </div>
    </div>

 <div class="control-group form-group">
        <div class="controls">  
            <label> Body </label>
                <textarea name="body" class="form-control" rows="15" cols="105" maxlength="500"><?php echo $row['body']; ?></textarea>
                <p class="help-block"></p>
        </div>
    </div>
<br/><br/>
<input type="submit" name="update" value="Save Changes"/>
<input type="submit" name="cancel" value="Cancel"/>
</center>
</div>
</form>

<?php 
        }
        ?>

updateinfo.php

<?php 
    include_once('db.php');
    if(isset($_POST['update']))
    {
        $title = $_POST['title'];
        $body = $_POST['body']; 
        $title = mysql_real_escape_string(htmlspecialchars($_POST['title']));
        $body = mysql_real_escape_string(htmlspecialchars($_POST['body']));

        $sql = ("UPDATE news SET title='$title', body='$body' WHERE id=".$_POST['id']) or die(mysql_error());

            if(mysql_query($sql))
            {
            echo "<script type='text/javascript'>alert('Changes saved!!');
                    window.location.assign('index.php');</script>";
            }
            else
            {
            echo "<script type='text/javascript'>alert('Error while updateing data.');
                    window.location.assign('index.php');</script>";
            }
    }
    if (isset($_POST['cancel']))
    {
        header("Location: index.php");
    }
?>

Please. I hope someone will help me. If this is too much, just please help me with the editinfo.php and I'll try to do the rest. Please guys.




Aucun commentaire:

Enregistrer un commentaire