Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do you update existing cell in a row in the MySQL table using PHP
    primarykey
    data
    text
    <p>I have MySQL Table that has two columns (name and description) in which I have already inserted some data using HTML Form. </p> <p>However I'm trying to figure out if there is a way if a user enters existing name and different description so that it updates the description for that existing name instead of adding new row with existing name and new description.</p> <p>Here is my PHP code:</p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;meta charset = "utf-8"&gt; &lt;title&gt;Search Results&lt;/title&gt; &lt;style type = "text/css"&gt; body { font-family: sans-serif; } table { background-color: lightblue; border-collapse: collapse; border: 1px solid gray; } td { padding: 5px; } tr:nth-child(odd) { background-color: white; } &lt;/style&gt; &lt;/head&gt; &lt;body&gt; &lt;?php $db_connect = mysql_connect("hostname", "name", "password"); $db_query = "SELECT * FROM urltable"; // connect to MySQL if (!$db_connect) { die("Could not connect to database"); } // open Products database if ( !mysql_select_db( "urls", $db_connect ) ) die( "Could not open products database &lt;/body&gt;&lt;/html&gt;" ); $db_insert = "INSERT INTO urltable(URL, description) VALUES ('$URL', '$Description')"; $db_insert_data = mysql_query($db_insert); if ($db_insert_data) { echo("&lt;br&gt;Input data is Added&lt;/br&gt;"); } else { echo("&lt;br&gt;Input data not Added&lt;/br&gt;"); } // query Products database if ( !( $result = mysql_query( $db_query, $db_connect ) ) ) { print( "&lt;p&gt;Could not execute query!&lt;/p&gt;" ); die( mysql_error() . "&lt;/body&gt;&lt;/html&gt;" ); } // end if mysql_close( $db_connect ); ?&gt;&lt;!-- end PHP script --&gt; &lt;table border="1"&gt; &lt;/br&gt; &lt;caption&gt;URL TABLE&lt;/caption&gt; &lt;/br&gt; &lt;?php // build table headings print( "&lt;tr&gt;" ); print("&lt;th&gt;URL&lt;/th&gt;"); print("&lt;th&gt;Description&lt;/th&gt;"); print( "&lt;/tr&gt;" ); // fetch each record in result set while ( $row = mysql_fetch_row( $result ) ) { // build table to display results print( "&lt;tr&gt;" ); foreach ( $row as $value ) print( "&lt;td&gt;$value&lt;/td&gt;" ); print( "&lt;/tr&gt;" ); } // end while ?&gt;&lt;!-- end PHP script --&gt; &lt;/table&gt; &lt;/br&gt; &lt;a href="insert.html"&gt;Home&lt;/a&gt;&lt;/br&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>Right now it will just insert any data that is entered, but I can't figure out how to do the update on description only if the existing name is entered. Could I use conditional statement to check for existing names?</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload