简体   繁体   中英

php update mysql one row no update

not sure where the issue is, I am including the, tried but not working at all, I am trying to update records from a database, however where every singe row is updated, only the first one which is name does not, can anybody help? I will be very grateful to get a fix, how do I fix it thank you. There used to be and id variable I used to pass it with GET however I am trying to make it with name variable, not working.

<!doctype html>
<html>
<head>
<title>Update</title>
</head>
<style>
input[type=text], select {
    width: 100%;
    padding: 8px;
    border: 1px solid #ccc;
    box-sizing: border-box; 
}

input[type=submit] {
    display:block;
    margin:0 auto;
    background-color: #4CAF50;
    color: white;
    padding: 14px 20px;
    border: none;
    cursor: pointer;
}

table {
    font-size: 80%;
    max-width: 500px;
    margin: auto;
}

body {
    margin: 0;
    font-family: Arial, sans-serif;
}

ul {
    list-style-type: none;
    margin: 0;
    padding: 0;
    overflow: hidden;
    font-weight: bold;
    background-color: #00ccff;
    text-align: center;
}

li {
    display: inline;
}

li a {
    display: inline-block;
    margin-right: -4px;
    color: white;
    text-align: center;
    padding: 16px;
    text-decoration: none;
}

li a:hover {
    background-color: #c1f0f0;
}
</style>
<header>
<ul>
  <li><a href="ad.php" style="text-decoration:none">Index</a>
  <li><a href="add.php" style="text-decoration:none">Add</a>
  <li><a href="list.php" style="text-decoration:none">Data</a>
  <li><a href="edit.php" style="text-decoration:none">Edit</a>
</ul>
</header>
<br>
<body>
<?php
define("server", "server");
define("username", "username");
define("password", "password");
define("database", "bdatabase");
$conn = mysqli_connect(server, username, password, database);
if(!$conn) {
die("Connection failed: " . mysqli_connect_error());
}

$name = $_GET['name'];
$sql = "SELECT name, description, download1, download2, download3, download4, download5, download6, author, authorweb, runson, license, image, list FROM g WHERE name='$name'";
$result = mysqli_query($conn, $sql);

if(isset($_POST['submit']))
{
    $name = $_POST['name'];
    $description = $_POST['description'];
    $download1 = $_POST['download1'];
    $download2 = $_POST['download2'];
    $download3 = $_POST['download3'];
    $download4 = $_POST['download4'];
    $download5 = $_POST['download5'];
    $download6 = $_POST['download6'];
    $author = $_POST['author'];
    $authorweb = $_POST['authorweb'];
    $runson = $_POST['runson'];
    $license = $_POST['license'];
    $image = $_POST['image'];
    $list = $_POST['list'];
    $sql = "UPDATE g SET name='$name', description='$description', download1='$download1', download2='$download2', download3='$download3', download4='$download4', download5='$download5', download6='$download6', author='$author', authorweb='$authorweb', runson='$runson', license='$license', image='$image', date=CURRENT_TIMESTAMP, list='$list' WHERE name='$name'";
    $result = mysqli_query($conn, $sql);
}

while($row = mysqli_fetch_assoc($result)) {
    $name = $row['name'];
    $description = $row['description'];
    $download1 = $row['download1'];
    $download2 = $row['download2'];
    $download3 = $row['download3'];
    $download4 = $row['download4'];
    $download5 = $row['download5'];
    $download6 = $row['download6'];
    $author = $row['author'];
    $authorweb = $row['authorweb'];
    $license = $row['license'];
    $runson = $row['runson'];
    $image = $row['image'];
    $list = $row['list'];
}
mysqli_close($conn);
?>

<form action="edit.php" method="post">
<table border="0" width="65">
<tbody>
<tr>
<td style="width: 65;">Name:</td>
<td style="width: 65;"><input name="name" id="textfield" size="65" type="text" value="<?php echo $name; ?>"></td>
</tr>
<tr>
<td style="width: 65;">Description:</td>
<td style="width: 65;"><textarea name="description" cols="46" rows="8" id="textarea"><?php echo $description; ?></textarea></td>
</tr>
<tr>
<td style="width: 65;">Download:</td>
<td style="width: 65;"><input name="download1" id="textfield" size="65" type="text" value="<?php echo $download1; ?>"</td>
</tr>
<tr>
<td style="width: 65;">Download:</td>
<td style="width: 65;"><input name="download2" id="textfield" size="65" type="text" value="<?php echo $download2; ?>"</td>
</tr>
<tr>
<td style="width: 65;">Download:</td>
<td style="width: 65;"><input name="download3" id="textfield" size="65" type="text" value="<?php echo $download3; ?>"</td>
</tr>
<tr>
<td style="width: 65;">Download:</td>
<td style="width: 65;"><input name="download4" id="textfield" size="65" type="text" value="<?php echo $download4; ?>"</td>
</tr>
<tr>
<td style="width: 65;">Download:</td>
<td style="width: 65;"><input name="download5" id="textfield" size="65" type="text" value="<?php echo $download5; ?>"</td>
</tr>
<tr>
<td style="width: 65;">Download:</td>
<td style="width: 65;"><input name="download6" id="textfield" size="65" type="text" value="<?php echo $download6; ?>"</td>
</tr>
<tr>
<td style="width: 65;">Author:</td>
<td style="width: 65;"><input name="author" id="textfield" size="65" type="text" value="<?php echo $author; ?>"></td>
</tr>
<tr>
<td style="width: 65;">Author Web:</td>
<td style="width: 65;"><input name="authorweb" id="textfield" size="65" type="text" value="<?php echo $authorweb; ?>"></td>
</tr>
<tr>
<td style="width: 65;">License:</td>
<td style="width: 65;"><input name="license" id="textfield" size="65" type="text" value="<?php echo $license; ?>"></td>
</tr>
<tr>
<td style="width: 65;">Runs on:</td>
<td style="width: 65;"><input name="runson" id="textfield" size="65" type="text" value="<?php echo $runson; ?>"></td>
</tr>
<tr>
<td style="width: 65;">Image location:</td>
<td style="width: 65;"><input name="image" id="textfield" size="65" type="text" value="<?php echo $image; ?>"></td>
</tr>
<tr>
<td style="width: 65;">List:</td>
<td style="width: 65;"><input name="list" id="textfield" size="65" type="text" value="<?php echo $list; ?>"></td>
</tr>
<tr>
<td><input type="hidden" name="id" value=<?php echo $_GET['name']; ?>></td>
</tr>
</tbody>
</table>
<input value="Submit" name="submit" type="submit">
</form>
<br>
</body>
</html>

Check if you don't reassign $name variable:

$name = $_POST['name'];
UPDATE g SET name='$name'

change variable name and try to pass it to db:

 $name_new = $_POST['name'];
UPDATE g SET name='$name_new'

when you are not inserting something in db while other tables insert the biggest possible scenario is that you are not passing anything, the other scenario is that your db 'name' table is not assigned for string, for example if you changed name of 'id' table to 'name', it will not save string. Try passing to db custom 'name' not from variable, like

"UPDATE g SET name='somewords'

if it saved, then problem is in your variable, if it does not save and others save, then it is your table conf

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