Can't access the Database (MAMP)

When I press my submit button, I get an error saying :

ErrorTable 'form2.demo' doesn't exist

form2 is my database name which is created in phpmyadmin.

I am a new MAC user.

Below is my php code.

define('DB_NAME','form2');
define('DB_USER','root');
define('DB_PASSWORD','root');
define('DB_HOST','localhost:8888');

My ports are :

Apache  : 8888
Msql : 8889

Full code is as below:

<?php
define('DB_NAME','form2');
define('DB_USER','root');
define('DB_PASSWORD','root');
define('DB_HOST','localhost:8889');


$link = mysql_connect(DB_HOST, DB_USER, DB_PASSWORD);

if(!$link){
  die('Could not connect :' . mysql_error());
}

$db_selected = mysql_select_db(DB_NAME, $link);

if(!$db_selected){
  die('Can\'t use' . DB_NAME . ':' . mysql_error());
}

$value = $_POST['input1'];

$sql = "INSERT INTO demo (input1) VALUES ('$value')";

if(!mysql_query($sql)){
  die('Error' . mysql_error());
 }
 mysql_close();
 ?>

Answers


Your problem have nothing to do with Mac. You have to show us more code. How the code in your form looks like? How you are sending stuff to database?

Let's assume that everything in code is set up properly. The main thing you have to check is if you actually have demo table in your database. Go to phpmyadmin and check it, if there is no table create it. You can use phpmyadmin to do that or do it via SQL query like that one

CREATE TABLE IF NOT EXISTS `form2`.`demo` ( `id` INT NOT NULL AUTO_INCREMENT , PRIMARY KEY (`id`));

Of course code above will create table with only ID column so you have to adjust it to your needs


 DEFINE('DB_USERNAME', 'root');
 DEFINE('DB_PASSWORD', '');
 DEFINE('DB_HOST', 'localhost');
 DEFINE('DB_DATABASE', 'customer');

 $mysqli = new mysqli(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_DATABASE);


 if (mysqli_connect_error()) {
  die('Connect Error ('.mysqli_connect_errno().') '.mysqli_connect_error());
 }

 echo 'Connected successfully.';


 $mysqli->close();

Try the msqli library instead


Need Your Help

Problem in SQL Server 2005

sql-server-2005

I know how to create tables in SQL server 2005. However due to system problems I reinstalled SQL Server 2005. After that I select the option like this: