0

I'm just a beginner in terms of programming, so I'm just referring all my codes through tutorials. Luckily, I found this online tutorial in youtube where users are allowed to add, update, and delete data in mysql using php. I follow all his instructions, I got it working but then it stopped when I added css on it.

This is not a general issue, I just need some help. If anyone can help me, much appreciated. Thank you so much.

<?php
    $servername = "localhost";
    $username = "root";
    $password = "";
    $dbname = "rssfeed";

    $connect = mysql_connect($servername, $username, $password, $dbname);
    if (!$connect) {
        die("Connection failed. Error" . mysql_error());
    }

    $database = mysql_select_db($dbname, $connect);
    if (!$database) {
        die("Can't select database");
    }

    $sql = "SELECT * FROM record";
    $data = mysql_query($sql, $connect);

    if (isset($_POST['update'])){
        $updateQuery = "UPDATE record SET name = '$_POST[name]', url = '$_POST[url]', description = '$_POST[desc]' WHERE name = '$_POST[hidden]'";
        mysql_query($updateQuery, $connect);
        header("Location: maintenance.php");
    };

    if (isset($_POST['delete'])){
        $deleteQuery = "DELETE FROM record WHERE name = '$_POST[hidden]'";
        mysql_query($deleteQuery, $connect);
        header("Location: maintenance.php");
    };

    if (isset($_POST['add'])){
        $addQuery = "INSERT INTO record (name, url, description) VALUES ('$_POST[iName]', '$_POST[iUrl]', '$_POST[iDesc]')";
        mysql_query($addQuery, $connect);
        header("Location: maintenance.php");
    };


    echo "<div class=center>
        <table id=myTable border=1>
        <tr>
        <th> Name </th>
        <th> URL </th>
        <th> Description </th>
        </tr>";

    while($record = mysql_fetch_array($data)) {
        echo "<form method=post action=maintenance.php>";
        echo "<tr>";
        echo "<td>" . "<input type=text name=name id=name value=" . $record['name'] . " </td>";
        echo "<td>" . "<input type=text name=url id=url value=" . $record['url'] . " </td>";
        echo "<td>" . "<textarea rows=1 cols=50 wrap=physical name=desc id=desc>" . strip_tags($record['description']) . "</textarea></td>";
        echo "<input type=hidden name=hidden value=" . $record['name'] . ">";
        echo "<td>" . "<input type=submit name=update id=update value=update" . " </td>";
        echo "<td>" . "<input type=submit name=delete id=delete value=delete" . " </td>";
        echo "</tr>";
        echo "</form>";


    }
        echo "</table>";
        echo "<table border=1>";
        echo "<form method=post action=maintenance.php>";
        echo "<tr>";
        echo "<td><input type=text name=iName></td>";
        echo "<td><input type=text id=url name=iUrl></input></td>";
        echo "<td><textarea rows=1 cols=50 name=iDesc></textarea></td>";
        echo "<td>" . "<input type=submit name=add id=add value=add" . " </td>";
        echo "</tr>";
        echo "</form>";
        echo "</table>";
        echo "</div>";

    mysql_close($connect);
    ?> 
Kamilah
  • 35
  • 2
  • 11

2 Answers2

0

In your queries, $_POST[name] should be $_POST[\"name\"]. BUT, this is terrible, you are very open to SQL injections.

Please have a read of this and stop using mysql_query (it's deprecated)

Community
  • 1
  • 1
rjdown
  • 9,162
  • 3
  • 32
  • 45
0

you have several errors in your echo output..

echo "<td>" . "<input type=text name=name id=name value=" . $record['name'] . " </td>";

You have forgot the single quotes for each html-element-attribute and the bigger then at the end of multiple input elements...

try this:

echo "<td><input type='text' name='name' id='name' value='" . $record['name'] . "'> </td>";
Giwwel
  • 347
  • 1
  • 4
  • I did that before but still doesn't work. My add button works perfectly, just the update and delete button. – Kamilah Dec 04 '14 at 16:23