0

I wrote a code with a form:

<form method="post">
<table>
<tr>
<td style="text-align:right">Artista:</td>
<td><input type="text" name="artist" /></td>
</tr>
<tr>
<td style="text-align:right">Titulo:</td>
<td><input type="text" name="title" /></td>
</tr>
<tr>
<td style="text-align:right">Capa (link):</td>
<td><input type="text" name="cover" /></td>
</tr>
<tr>
<td colspan="2">
<center>
<input type="submit" style="margin-top:20px" />
</center>
</td>
</tr>
</table>
</form>

Then I wrote some php to get the info on form and put it on a mySQL database.

if (isset($_POST['submit'])){
   $dbhost = 'HOST';
   $dbuser = 'USER';
   $dbpass = 'PASS';
   $conn = mysql_connect($dbhost, $dbuser, $dbpass);
   $artist = strtoupper($_POST[artist]);
   $title = strtoupper($_POST[title]);
   $cover = $_POST[cover];
   if(! $conn )
   {
  die('Could not connect: ' . mysql_error());
   }
   $sql = "INSERT INTO 'MusicList'('artist', 'title', 'cover', 'votes') VALUES ('".$artist."', '".$title."', '".$cover."', 0)";
mysql_select_db('DATABASE_NAME');
   $retval = mysql_query( $sql, $conn );
   if(! $retval )
   {
  die('Could not enter data: ' . mysql_error());
   }
   echo "<center><h1>Success!</h1></center>";
   mysql_close($conn);
   }

However it seems something has gone wrong because it doesn't write anything on SQL. Can you tell me what did I wrote wrong?

Cœur
  • 37,241
  • 25
  • 195
  • 267

1 Answers1

1

Change the single quotes around MusicList to be backticks, and get rid of the single quotes around the column names like this:

$sql = "INSERT INTO `MusicList`(artist, title, cover, votes) VALUES ('".$artist."', '".$title."', '".$cover."', 0)";

SQL isn't expecting quotes here.

Also as Fred pointed out, you need to name your submit button, or else the if statement will always be false.

So something like:

<input type="submit" name="submit_button" style="margin-top:20px" />

and change the if statement to:

if (isset($_POST['submit_button'])){
Chris Trudeau
  • 1,427
  • 3
  • 16
  • 20