Jump to content

[SOLVED] I can't upload information from form to mysql database


contra10

Recommended Posts

for some reason it processes but the information isn't submitted to mysql here is my code

<?php 
// Connects to your Database 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("inform") or die(mysql_error()); 

//This code runs if the form has been submitted
if (isset($_POST['submit'])) { 

//This makes sure they did not leave any fields blank
if (!$_POST['name'] | !$_POST['group'] | !$_POST['description']) {
die('You did not complete all of the required fields');
}

// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['name'] = ($_POST['name']);
}
$groupcheck = $_POST['name'];
$check = mysql_query("SELECT name FROM groups WHERE name = '$groupcheck'") 
or die(mysql_error());
$check2 = mysql_num_rows($check);

//if the name exists it gives an error
if ($check2 != 0) {
die('Sorry, the group '.$_POST['name'].' is already in use.');
}

// this makes sure both passwords entered match
if ($_POST['name'] == $_POST['group']) {
die('Input a different name please. ');
}

// now we insert it into the database
$insert = "INSERT INTO groups (name, group, description, city, email)
VALUES ('".$_POST['name']."', '".$_POST['group']."', '".$_POST['description']."', '".$_POST['city']."', '".$_POST['email']."', '".$_POST['image']."')";
$add_group = mysql_query($insert);

?>

			<h1>Good Job</h1>
<p>Thank you<a href= "http://localhost/main"</a>Main</p>

<?php 
} 
else 
{ 
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<table border="0" align="center" width="100">
<td align="left" width="20"><b>Name:</td><td><input type="text" name="name" maxlength="30" STYLE="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;"></td><br>
<tr><td align="left"><b>Category:</td><td><select name="group" type="group" STYLE="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;">
<option value="Alumni" selected>Alumni</option>
<option value="Automotive">Automotive</option>
<option value="Business">Business</option>
<option value="City">City</option>
<option value="Environmental">Environmental</option>
<option value="Fashion">Fashion</option>
<option value="Angola">Angola</option>
</select></td></tr>
<td><b>Description:</b></td>
<td><textarea name="description" type="description" cols="50" rows="7" maxlength="10" STYLE="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;">PLEASE GIVE A DESCRIPTION OF THE GROUP</textarea></td></tr>
<td><b>City:</b></td>
<td><input type="city" name="city" maxlength="50" STYLE="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;"></td>
<tr><td><b>Email:</td>
<td><input type="email" name="email" maxlength="50" STYLE="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;"></td>
<tr><td><b>Image:</td>
<td><input type="file" name="image" name="groupimage" STYLE="color: #FFFFFF; font-family: Verdana; font-weight: bold; font-size: 12px; background-color: #72A4D2;"></td>
<tr><td></td>
<td>Uploaded Image</td>
<td>
<input type="submit" name="submit">
</td>
</tr>
</table>
</form>
<?php
}
?>

please help

Link to comment
Share on other sites

nvm

 

error is

 

 

Create A Group qwe You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'group, description, city, email) VALUES ('qwe', 'Alumni', 'PLEASE GIVE A DESCRI' at line 1

 

Link to comment
Share on other sites

i don't think i did it right

 

<?php 
// Connects to your Database 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("inform") or die(mysql_error()); 

//This code runs if the form has been submitted
if (isset($_POST['submit'])) { 

//This makes sure they did not leave any fields blank
if (!$_POST['name'] | !$_POST['group'] | !$_POST['description']) {
die('You did not complete all of the required fields');
}

// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['name'] = ($_POST['name']);
}
$groupcheck = $_POST['name'];
$check = mysql_query("SELECT name FROM groups WHERE name = '$groupcheck'") 
or die(mysql_error());
$check2 = mysql_num_rows($check);

//if the name exists it gives an error
if ($check2 != 0) {
die('Sorry, the group '.$_POST['name'].' is already in use.');
}

// this makes sure both passwords entered match
if ($_POST['name'] == $_POST['group']) {
die('Input a different name please. ');
}
$name = mysql_real_escape_string($_POST['name']);
$group = mysql_real_escape_string($_POST['group']); 
$description = mysql_real_escape_string($_POST['description']);
$city = mysql_real_escape_string($_POST['city']);
$email = mysql_real_escape_string($_POST['email']);

// now we insert it into the database
$insert = "INSERT INTO groups {name,group,description,city, email}
VALUES ('".$name."', '".$group."', '".$description."', '".$city."', '".$email."')";
$add_group = mysql_query($insert) or die(mysql_error());	
?>

Link to comment
Share on other sites

$insert = "INSERT INTO groups {name,group,description,city, email}
VALUES ('$name', '$group', '$description', '$city', '$email')";
$add_group = mysql_query($insert) or die(mysql_error()); 

 

If you wrap the statement in " " (quotation marks) then you can use apostrophe's  ' ' to wrap the value items. They will still be parsed as variables.

 

Do you get any errors after doing this??

 

Nate

Link to comment
Share on other sites

the error i get is

 

Create A Group qwe You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '{name,group,description,city, email} VALUES ('qwe', 'Alumni', 'PLEASE GIVE A DE' at line 1

 

heres the code again

 

<?php 
// Connects to your Database 
mysql_connect("localhost", "root", "") or die(mysql_error()); 
mysql_select_db("inform") or die(mysql_error()); 

//This code runs if the form has been submitted
if (isset($_POST['submit'])) { 

//This makes sure they did not leave any fields blank
if (!$_POST['name'] | !$_POST['group'] | !$_POST['description']) {
die('You did not complete all of the required fields');
}

// checks if the username is in use
if (!get_magic_quotes_gpc()) {
$_POST['name'] = ($_POST['name']);
}
$groupcheck = $_POST['name'];
$check = mysql_query("SELECT name FROM groups WHERE name = '$groupcheck'") 
or die(mysql_error());
$check2 = mysql_num_rows($check);

//if the name exists it gives an error
if ($check2 != 0) {
die('Sorry, the group '.$_POST['name'].' is already in use.');
}

// this makes sure both passwords entered match
if ($_POST['name'] == $_POST['group']) {
die('Input a different name please. ');
}
$name = mysql_real_escape_string($_POST['name']);
$group = mysql_real_escape_string($_POST['group']); 
$description = mysql_real_escape_string($_POST['description']);
$city = mysql_real_escape_string($_POST['city']);
$email = mysql_real_escape_string($_POST['email']);

// now we insert it into the database
$insert = "INSERT INTO groups {name,group,description,city, email}
VALUES ('$name', '$group', '$description', '$city', '$email')";
$add_group = mysql_query($insert) or die(mysql_error());	
?>

Link to comment
Share on other sites

Well, the query appears to by correct syntax....

 

Do this and post what you get. I am wondering if your database structure is not accepting the data....

 

Post the results and we'll go from there.

$insert = "INSERT INTO groups {name,group,description,city, email}
VALUES ('$name', '$group', '$description', '$city', '$email')";
echo $insert;
//$add_group = mysql_query($insert) or die(mysql_error()); 

Link to comment
Share on other sites

Ok, that looks right to me as far as the syntax.....

 

I would start looking at the database and make sure that your field names are correct and that the data types for those fields are correct. I am not sure if you will get this particular error on say a varchar(5)  field when the data is longer.

 

Take that same query into PHPmyAdmin and see if you can get it to insert there.

 

If you get the same error (probably will) then look at the field names and the data type/length for those fields.

 

Nate

Link to comment
Share on other sites

k, i did that before and before the only data that would enter is the name, after i did it just for the city, diescription ... and on its own none of them would enter. my names were id, name, group, description... all with text except for id which was an integer....i changes it to varchar now and added lengths to them and for some reason still shows that same problem...I also checked the fields and the names in the post all match , i honestly don't know what the problem could be.

Link to comment
Share on other sites

-- phpMyAdmin SQL Dump

-- version 2.11.6

-- http://www.phpmyadmin.net

--

-- Host: localhost

-- Generation Time: Dec 21, 2008 at 09:54 PM

-- Server version: 5.0.51

-- PHP Version: 5.2.6

 

SET SQL_MODE="NO_AUTO_VALUE_ON_ZERO";

 

--

-- Database: `inform`

--

 

-- --------------------------------------------------------

 

--

-- Table structure for table `groups`

--

 

CREATE TABLE `groups` (

  `name` varchar(20) NOT NULL,

  `group` varchar(20) NOT NULL,

  `description` varchar(150) NOT NULL,

  `city` varchar(20) NOT NULL,

  `email` varchar(20) NOT NULL

) ENGINE=InnoDB DEFAULT CHARSET=latin1;

 

--

-- Dumping data for table `groups`

--

 

 

Link to comment
Share on other sites

I AM AN IDIOT!!!!!!!.... Can't believe I missed this.

 

Your using a mysql reserved word in your table. the word group is reserved. In order for it to work you will have to use backticks around it.  Also, your using curly brackets in the column name part.. you have to use ( ) not { }.

 

The error you were getting was because of the curly brackets ... but once those got fixed you would have gotten an error about using group without backticks.

 

This works for me without errors.

 

INSERT INTO groups (name,`group`,description,city, email) VALUES ('yhu', 'Alumni', 'PLEASE GIVE A DESCRIPTION OF THE GROUP', 'uyh', 'yhu')

 

Nate

Link to comment
Share on other sites

OH MY GOD.....THANK YOU i didn;t realize i was using the other brackets, and thanks for telling me about the ` ` around the reserved words i honestly did not know that....on last question may be stupid but i need to make sure can i still add and id section thats auto incrementerd....

 

I know i prolly can but im so messed up right now...

 

i love this site...need to donate soon...LOL thank u very very much

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.