Click here to Skip to main content
15,906,947 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to insert and this is the result that i am getting.

has it trying to insert but hitting a snagg instead. how can i fix this. here is the error i am recieving

PHP
0){
    echo "";
}
else
{//insert data into table
    $sql = "INSERT INTO membersVALUES('$title', '$firstname', '$lastname', '$username', '$email', '$gender', '$services', '$address', md5('$mypwd'))";
    if(mysqli_query($conn,$sql)){echo "";
    $row=mysqli_fetch_array($result);
    mysqli_close($conn);
}
else
{
    echo "Error inserting values into database";
}//end of line
}
}
*/ ?> 


and here it what i have tried. a third eye and any help will be much appreciated thanks

What I have tried:

PHP
<pre><?php<?php

		
		 if(isset($_POST['Enter']))
		{
			
			//Capture values from form and store in php variables
			$title=$_POST['title'];
			$firstname=$_POST['firstname'];
			$lastname=$_POST['lastname'];
			$username=$_POST['username'];
			$email=$_POST['email'];
			$gender =$_POST['gender'];
			$services=join(",", $_POST['services']);                                                                                                                                    
			$address=$_POST['address'];
			$mypwd=$_POST['mypwd'];
			
		

			//Create and check connection to the server
			include'db_server.php';
				
			
				
			//Query the database 
			$sql="SELECT * FROM members WHERE username='$username'";
			
			$result=mysqli_query($conn,$sql) or die("Error:" .mysqli_error());
			
			$rowcount=mysqli_num_rows($result);
			
				if($rowcount>=1)
				{
					echo "<script type=\"text/javascript\">
							alert('Username already exist');
							window.location=\"../xhtml/login_user.html\";
						</script>";
				}
				else
				{
					//insert data into table
					$sql="INSERT INTO members 
						  VALUES('$title', '$firstname', '$lastname', '$username', '$email', '$gender', '$services', '$address', md5('$mypwd'))";
					
					if(mysqli_query($conn, $sql))
					{
						 $row=mysqli_fetch_array($result);
						mysqli_close($conn);
					
						echo "<script type=\"text/javascript\">
								 alert('Welcome!! $firstname $lastname, you are now a member of the Caribbean Nature Seekers Institute TT(CNSITT)');
								window.location=\"../xhtml/congrats.html\";
							</script>";
					}
				
					else
					{
						echo "Error inserting data in the members table";
										
					}
				}	
		}
Posted
Updated 18-Apr-19 21:17pm
v2

Quote:
here is the error i am recieving

You don't show any error message, just a dump of code!
May be using the debugger will allow you to understand what is going on.

PHP
$sql="INSERT INTO members VALUES('$title', '$firstname', '$lastname', '$username', '$email', '$gender', '$services', '$address', md5('$mypwd'))";

Not a solution to your question, but another problem you have.
Never build an SQL query by concatenating strings. Sooner or later, you will do it with user inputs, and this opens door to a vulnerability named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input a name like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability, and the crash is the least of the problems, a malicious user input and it is promoted to SQL commands with all credentials.
SQL injection - Wikipedia[^]
SQL Injection[^]
SQL Injection Attacks by Example[^]
PHP: SQL Injection - Manual[^]
SQL Injection Prevention Cheat Sheet - OWASP[^]
How can I explain SQL injection without technical jargon? - Information Security Stack Exchange[^]
 
Share this answer
 
v2
We can't tell - we don't have access to your data, or to the DB you are trying to INSERT to - and both are needed in order to work out what is wrong.

So start by doing what Patrice sugestoes and converting your whole app to parameterised queries, then begin looking at the DB table and the order of columns.
I'd guess that you have an automatically generated ID column at the start - an IDENTITY column - and that is causing the problem.
If so, the solution is to ALWAYS list the columns you wish to insert data into, in the order you will provide data:
SQL
INSERT INTO MyTable (Column2, Column3) VALUES (Value2, Value3)
If you don't, SQL will start from the first column and just move on. If the first is an IDENTITY column, you can't specify it;'s value, and the INSERT will be rejected.
 
Share this answer
 
Comments
divinity02 19-Apr-19 6:33am    
@originalGriff, how can u upload a picture on this site, I would like to upload a pic of the db i am trying to insert into
OriginalGriff 19-Apr-19 6:46am    
You can't. There are some who would delight in posting "inappropriate" pictures and we don't have enough volunteers to moderate every single post.

And why would I want an image of anything? A copy'n'paste of the table definition as text could be pasted into SQL to create the same table for testing ... don't be so lazy! :laugh:
divinity02 19-Apr-19 7:53am    
ok so here is my table in sequence.

title, firstname, lastname, username,email, gender,services, address, mypwd

that is how my table has set up and I am not lazy eh, have been at this assignment for a long time and it is the insert has been the problems, it was inserting at one time and for some reason it stop inserting and have not been retrieve

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900