0

I have a button that when pressed will create a table in the database. I tried this

index.html

<button id="loadButton" type="button" class="btn btn-success" style="margin-top:5px;">Carregar Base de Dados</button>

dash.js

$('#loadButton').click(function() {
    $.ajax({
        url: 'connectdb.php'
    });
});

connectdb.php

<?php

$server = 'localhost';
$user = 'root';
$password = '*****';
$database = 'test';

$con = mysql_connect($server, $user, $password, $database);

if (!$con) {
  die('error: ' . mysql_error($con));
}

// Create table
$sql="CREATE TABLE Post( 
    id_post int Primary Key, 
    titulo varchar(100) not null, 
    imagem longblob,
    descricao varchar(1000) not null,
    hashtag varchar(100) not null
)";

// Execute query
mysql_query($con,$sql))
?>

?>

but when I check my database table was not created. I'm new in jquery, what I'm doing wrong. can someone help me?

3
  • mysql_query -> mysql_query($sql,$link), mysqli_query -> mysqli_query($link,$sql). Update to mysqli. Also, you may want to use CREATE TABLE IF NOT EXISTS Posts, as if you click again it will overwrite your table. Commented Aug 2, 2014 at 16:54
  • 1
    Please use MySQLi. MySQL is officially deprecated. Commented Aug 2, 2014 at 16:55
  • 1
    Why would you create a table for every post? Additionally, this is bad practice anyway. I would also recommend using English names for columns. While end user localized text is a good thing, IMHO code should always use English, as it's the language of business in today's world. Commented Aug 2, 2014 at 17:00

3 Answers 3

1

Try this and if you want to be shure the php code is called replace all the code with an simple echo and check the response.

$server = 'localhost';
$user = 'root';
$password = '*****';
$database = 'test';

$db = new mysqli($server, $user, $password, $database);

if($db->connect_errno > 0){
    die('Unable to connect to database [' . $db->connect_error . ']');
}

$db->query("
CREATE TABLE Post( 
    id_post int Primary Key, 
    titulo varchar(100) not null, 
    imagem longblob,
    descricao varchar(1000) not null,
    hashtag varchar(100) not null
)");
Sign up to request clarification or add additional context in comments.

Comments

1

First : Please use MySQLi* mysql_* is deprecated.

You have errors in your .php code

mysql_query($con,$sql))
  • one ) of )) to much
  • you should have a ; at the end of your command
  • wrong order $con,$sql

this is OK

mysql_query($sql,$con);

sometimes you should use the database here 'test' in your query

$database = 'test';
...
$sql="CREATE TABLE test.Post( 

you should test your result

$result = mysql_query($sql,$con);
if (!$result) {
    $message  = 'invalid query: ' . mysql_error() . "\n";
    $message .= 'your query: ' . $sql;
    echo $message;
    die($message);
}

To search for errors this is very helpful !

Comments

-1

go to connectdb.php directly e.g. http://www.example.com/connectdb.php, fix all the errors and make sure it is working first.

3 Comments

How would I know what errors he is getting when he did not post his errors messages? I have provided a debugging method as I am guessing he has errors in connectdb.php although I cannot know for sure.
Ask that in a comment. I don't consider this a valid answer.
I respect your judgement, but I don't intend to know what error he gets as I think he is capable of fixing the errors, just that he cannot see the error messages because it is an AJAX call and he is new to jQuery. Thus I am certain that by going directly to connectdb.php will answer "what I'm doing wrong. can someone help me?"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.