MySQL - Can't save data on a table

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

gilmarzinho
New php-forum User
New php-forum User
Posts: 1
Joined: Thu Dec 20, 2012 5:08 am

MySQL - Can't save data on a table

Postby gilmarzinho » Thu Dec 20, 2012 5:29 am

Hello. I need help, folks.

The HTML code follows:

Code: Select all

<html><head><title>Bank 01</title></head><body>
<br/>
<form method="POST" action="register.php">
Band: <input type="text" name="band"><br/>
Album: <input type="text" name="album"><br/>
Year: <input type="text" name="year"><br/>
<input type="submit" value="Ok">
</form>
</body></html>


The PHP code:

Code: Select all

<?php

   $band = $_REQUEST['band'];
   $album = $_REQUEST['album'];
   $year = $_REQUEST['year'];

   $url = "localhost";
   $user = "root";
   $pass = "";
   
   if(!($id_conex = mysql_connect("$url", "$user", "$pass"))){
      echo "MySQL error.";
      exit;
   }
   
   $dbname= "cds";
   
   if(!($conex = mysql_select_db($dbname, $id_conex))){
      echo "Can't connect database.";
      exit;
   }
      
   $sql = "INSERT INTO coll(banda, disco, ano) VALUES ($banda, $disco, $ano)";
   
   $res = mysql_query($sql);
   
   mysql_close($id_conexao);

?>


The SQL script:

Code: Select all

CREATE DATABASE cds;
USE cds;
CREATE TABLE IF NOT EXISTS `coll` (
  `id_cds` int(11) NOT NULL AUTO_INCREMENT,
  `band` varchar(20) DEFAULT NULL,
  `album` varchar(30) DEFAULT NULL,
  `year` int(4) DEFAULT NULL,
  PRIMARY KEY (`id_cds`)
) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=2 ;


Simply don't work, and I don't know why.
Anyone can help?

Thanks folks!
Té.

jp26198926
New php-forum User
New php-forum User
Posts: 5
Joined: Mon Apr 30, 2012 5:48 am

Re: MySQL - Can't save data on a table

Postby jp26198926 » Thu Dec 20, 2012 6:35 pm

$sql = "INSERT INTO coll(banda, disco, ano) VALUES ($banda, $disco, $ano)";


cant find these fields (i.e banda,disco, ano) in your sql script.
also these variables (i.e $banda, $disco, $ano)

User avatar
simplypixie
php-forum Active User
php-forum Active User
Posts: 300
Joined: Sun Dec 11, 2011 12:51 am
Location: Shrewsbury, Shropshire
Contact:

Re: MySQL - Can't save data on a table

Postby simplypixie » Sat Dec 22, 2012 12:38 am

Tray adding a space between your table name and the column names, add quote marks around your variables (plus your variables don't match the variable names above) and add mysql_error to the end to display any errors that may be happening:

Code: Select all

$sql = "INSERT INTO coll (banda, disco, ano) VALUES ('$band', '$album', '$year')" or die(mysql_error());

sudheer
New php-forum User
New php-forum User
Posts: 28
Joined: Wed Dec 21, 2011 1:41 am

Re: MySQL - Can't save data on a table

Postby sudheer » Tue Dec 25, 2012 8:44 pm

Hi,

Try below code

$sql = mysql_query("INSERT INTO coll (banda, disco, ano) VALUES ('".$_REQUEST['band']."','".$_REQUEST['album']."','".$_REQUEST['year']."')");


Return to “PHP coding => General”

Who is online

Users browsing this forum: No registered users and 2 guests

cron