Tarik Jones Tarik Jones - 4 months ago 7
SQL Question

Why am I getting an error? (SQL syntax)

I am trying to create a table in a database, but it is not working. I am getting this error:
"Error creating group: You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'desc VARCHAR(3000) NOT NULL, addmem VARCHAR(300) )' at line 4"

Here is my HTML and PHP:

<form action="includes/creategroup.inc.php" method="POST">
<div>
<label>Title<span class="req">*</span></label>
<input type="text" name="title" required autocomplete="off" />
</div>
<div>
<label>Description<span class="req">*</span></label>
<input type="text" name="desc" required autocomplete="off" />
</div>
<div>
<label>Add members<span class="req">*</span></label>
<input type="text" name="addmem" placeholder="Enter Username" required autocomplete="off" />
<?php
$add = "SELECT COLUMN_NAME, TABLE_NAME
FROM INFORMATION_SCHEMA.COLUMNS
WHERE COLUMN_NAME = 'userid'"
?>
</div>
<button type="submit" class="button button-block"/>Create Group</button>
</form>


Here is my PHP and SQL:

<?php
$conn = mysqli_connect("localhost", "root", "", "scholastus");
if (!$conn){
die("Connection failed: ".mysqli_connect_error());
}

session_start();

$title = $_POST['title'];
$desc = $_POST['desc'];
$addmem = $_POST['addmem'];

// sql to create table
$sql = "CREATE TABLE groups(
group_id INT(6) NOT NULL PRIMARY KEY AUTO_INCREMENT,
title VARCHAR(140) NOT NULL,
desc VARCHAR(3000) NOT NULL,
addmem VARCHAR(300)
)";

if (mysqli_query($conn, $sql)){
echo "Group created successfully";
} else {
echo "Error creating group: " . mysqli_error($conn);
}

// Close connection
mysqli_close($conn);

Answer

DESC is a reserved word; if you want to use it, it must be delimited.

https://dev.mysql.com/doc/refman/5.5/en/keywords.html

Comments