简体   繁体   中英

update mysql row with html form and php

I've been looking through many threads on here without finding a solution to my problem. I've created a form that is supposed to show content of a database in input boxes, and when i change the content, it should be updated in the database.

No errors, nothing gets changed.

<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno()){
    echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));

while($row = mysqli_fetch_array($result)){
    echo '<form action="" method="post">';
    echo '<div style="float:left">';
    echo '<table border="1" bordercolor="#000000">';
    echo '<tr>';
    echo '<td>link</td>';
    echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td>img</td>';
    echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td>tekst</td>';
    echo '<td><input type="text" name="imgid" value="'.$row['name'].'"></td>';
    echo '</tr>';
    echo '<tr>';
    echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
    echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
    echo '</tr>';
    echo '</table></div>';
    echo '<div style="float:left"><a href="'.$row['link'].'"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></a></div>';
    echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
}

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

    $id = $_POST['id'];
    $link = $_POST['linkid'];
    $img = $_POST['imgid'];
    $name = $_POST['nameid'];

    $sql = mysqli_query("UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'");

    $retval = mysqli_query( $sql, $con );

    if(! $retval ){
        die('Could not update data: ' . mysql_error());
    }

    echo "Updated data successfully\n";
}

mysqli_close($con);
?>

The form show the database content fine, but nothing happens when changed.

I appreciate any help I can get.


This is what it looks like now.

<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

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

$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];

$sql = mysqli_query("UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'");

$retval = mysqli_query( $con, $sql );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";


}

$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));

while($row = mysqli_fetch_array($result))
  {
  echo '<form action="" method="post">';
  echo '<div style="float:left">';
  echo '<table border="1" bordercolor="#000000">';
  echo '<tr>';
  echo '<td>link</td>';
  echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
  echo '</tr>';
  echo '<tr>';
  echo '<td>img</td>';
  echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
  echo '</tr>';
  echo '<tr>';
  echo '<td>tekst</td>';
  echo '<td><input type="text" name="nameid" value="'.$row['name'].'"></td>';
  echo '</tr>';
  echo '<tr>';
  echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
  echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
  echo '</tr>';
  echo '</table></div>';
  echo '<div style="float:left"><a href="'.$row['link'].'"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></a></div>';
  echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
  }



mysqli_close($con);
?>

Now i get this error.

Warning: mysqli_query() expects at least 2 parameters, 1 given in /Applications/XAMPP/xamppfiles/htdocs/page/admin.php on line 17

Warning: mysqli_query(): Empty query in /Applications/XAMPP/xamppfiles/htdocs/page/admin.php on line 19 Could not update data:

Because your udate is at the end of the page put it above the rest.

And also change isset($_POST['update'] to isset($_POST['gem']

<?php
$con=mysqli_connect("localhost","root","","frontpage");
// Check connection
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

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

$id = $_POST['id'];
$link = $_POST['linkid'];
$img = $_POST['imgid'];
$name = $_POST['nameid'];

$sql = "UPDATE frontpage_left_links SET link = '$link', img = '$img', name = '$name' WHERE id = '$id'";

$retval = mysqli_query($con,$sql );
if(! $retval )
{
  die('Could not update data: ' . mysql_error());
}
echo "Updated data successfully\n";


}

$result = mysqli_query($con,"SELECT * FROM frontpage_left_links")
or die("Error: ".mysqli_error($con));

while($row = mysqli_fetch_array($result))
  {
  echo '<form action="" method="post">';
  echo '<div style="float:left">';
  echo '<table border="1" bordercolor="#000000">';
  echo '<tr>';
  echo '<td>link</td>';
  echo '<td><input type="text" name="linkid" value="'.$row['link'].'"></td>';
  echo '</tr>';
  echo '<tr>';
  echo '<td>img</td>';
  echo '<td><input type="text" name="imgid" value="'.$row['img'].'"></td>';
  echo '</tr>';
  echo '<tr>';
  echo '<td>tekst</td>';
  echo '<td><input type="text" name="imgid" value="'.$row['name'].'"></td>';
  echo '</tr>';
  echo '<tr>';
  echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';
  echo '<td><input type="hidden" name="id" value="'.$row['id'].'"></td>';
  echo '</tr>';
  echo '</table></div>';
  echo '<div style="float:left"><a href="'.$row['link'].'"><center><img src="img/'.$row['img'].'"><br />'.$row['name'].'</center></a></div>';
  echo '</form><br /><br /><br /><br /><br /><br /><br /><br />';
  }



mysqli_close($con);
?>

try to replace :

echo '<td><input type="submit" id="update" name="gem" value="Gem"</td></td>';

with :

echo '<td><input type="submit" id="update" name="update" value="Gem"</td></td>';

In your form you are having update button name as gem but you are using if(isset($_POST['update'])) to run update query.

Change it to if(isset($_POST['gem']))

The easiest way would be to simply change the name attribute on your submit to the $_POST[] variable you used. name="update"The easiest way would be to simply change the name attribute on your submit to the $_POST[] variable you used. name="update"

Edit :: Answered already.

echo '<td><input type="submit" id="update" name="update" value="Gem"</td></td>';

只需从上面删除第二个TD!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM