greggustin Posted July 12, 2006 Share Posted July 12, 2006 I am brand new at thisok with HTML // learning PHPtrying the most SIMPLE tests for my selfmade at DB using phpMyAdmin installed on my PC (WinXP)c:\wamp\mysqlc:\wamp\phpc:\wamp\phpMyAdminandc:\wamp\www = localhost(ie i put my index.html in that folder and 'surf' to localhost and all is fine :) )I put this file in same directory (eg my2.html)and surf to [http://localhost/my2.html ]and it looks okhere is html file[code]<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><title>Untitled</title></head><body><form action="process.php" method="post">Your Name: <input type="text" name="name"><br>E-mail: <input type="text" name = "email"><br>Location: <input type="text" name = "location"><br><input type="submit" value="Submit"></form></body></html>[/code]and here is the code for process.php[code]<head><title>Untitled</title></head><body><?php$name=$_POST['name'];$email=$_POST['email'];$location=$_POST['location'];MySQL_connect("localhost:/MySQL/data/test1", "root", "")or die ('I cannot connect to the database because: ' . MySQL_error());MySQL_select_db("test1");MySQL_query("INSERT INTO `test1table` VALUES ('$name', '$email', '$location')");Print "Your information has been successfully added to the database.";?></body></html>[/code]it also appears ok in browserI get this as echo on screen (ie NOT the error message)[b]Your information has been successfully added to the database.[/b]I am guessing the problem is in the HOST field above in the 'msql_connect' lineand yes - there is a DB called "test1" and a table called "test1table.frm"which is a folder called test1 which is in the data folder (as above)I have been reading tutorials for hoursthey say DB "locations" (and something about 'socks' :P )so = what is syntax for my location???2nd question - as this is not working = why does it show the 'success' echo when I submit the data?(I open the DB using phpMyAdmin and there are no new data rows) Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 12, 2006 Share Posted July 12, 2006 Change:[code]MySQL_connect("localhost:/MySQL/data/test1", "root", "")or die ('I cannot connect to the database because: ' . MySQL_error());[/code]To[code]MySQL_connect("localhost", "root", "")or die ('I cannot connect to the database because: ' . MySQL_error());[/code] Quote Link to comment Share on other sites More sharing options...
robos99 Posted July 12, 2006 Share Posted July 12, 2006 put "or die" after all your sql functions. my guess is either your select_db is failing or the query itself is failing.you can also test to see if MySQL successfully inserted your data by doing this:[code]if(mysql_affected_rows($connection)==0){ echo "Insertion failed."; }[/code]Also put this in at the top of your script when debugging:[code]error_reporting(E_ALL);[/code]This should give you some info on what is going wrong.To answer your other question, the reason it's saying that it added the info is because you don't have an "or die" statement after your query or select_db. So even if it did fail, PHP just continues and echos whatever you tell it to. That's why you want to put "or die" after everything...actually it'd be even better to write an error handling function but that may be over your head. Quote Link to comment Share on other sites More sharing options...
greggustin Posted July 12, 2006 Author Share Posted July 12, 2006 [quote author=hackerkts link=topic=100261.msg395533#msg395533 date=1152669104]Change:[code]MySQL_connect("localhost:/MySQL/data/test1", "root", "")or die ('I cannot connect to the database because: ' . MySQL_error());[/code]To[code]MySQL_connect("localhost", "root", "")or die ('I cannot connect to the database because: ' . MySQL_error());[/code][/quote]well - this was my 1st try - and it did not workthanks anywayhmmas local host = c:\wamp\wwwand MySql is up a level then downegc:\wamp\MySqlhow does the connect command know where to find the DB?should I copy it 'under' the localhost folder somewhere? Quote Link to comment Share on other sites More sharing options...
greggustin Posted July 12, 2006 Author Share Posted July 12, 2006 I added the 'or die' after each line as suggested and got this after I hit submit[i]I cannot connect to the database because: Column count doesn't match value count at row 1[/i]my table has 4 fields, (id, name, email and location)'id' is auto increment = so I did not put that field name in my php fileshould I?and if so, how should I edit my insert values code? Quote Link to comment Share on other sites More sharing options...
hackerkts Posted July 12, 2006 Share Posted July 12, 2006 Last solution, try to use wamp -> http://www.wampserver.com/enYou just put "localhost" and it will be working.[code]mysql_connect("localhost", "root","")or die(mysql_error());mysql_select_db("database-name-here") or die(mysql_error());[/code] Quote Link to comment Share on other sites More sharing options...
greggustin Posted July 12, 2006 Author Share Posted July 12, 2006 [quote author=hackers link=topic=100261.msg395547#msg395547 date=1152673627]Last solution, try to use wamp -> http://www.wampserver.com/enYou just put "localhost" and it will be working.[code]MySQL_connect("localhost", "root","")or die(MySQL_error());MySQL_select_db("database-name-here") or die(MySQL_error());[/code][/quote]heheI am already using wamp server :)anyway got it just about workinghad to add another field called IDbut I had to hard code the value to make it work and have to increment it by hand[code]$id=7;[/code]I am sure there is some code to make it do that automaticallythe real purpose of this exercise is to allow web site visitors the option of checkig off check boxesand therfore = every field will not be 'filled'will that be a problem? Quote Link to comment Share on other sites More sharing options...
greggustin Posted July 12, 2006 Author Share Posted July 12, 2006 generic question = when you do the 'query insert'do you have to have a variable for EVERY one of the columns in the table?if so. seems odd ? Quote Link to comment Share on other sites More sharing options...
criticalsystems Posted July 12, 2006 Share Posted July 12, 2006 you do not have to have a variable for ever column you can just specify the cols that you are going to give values for in the SQL statement.eg INSERT into table (col1,col2,col3) values (col1val,col2val,col3val); Quote Link to comment Share on other sites More sharing options...
saikiran Posted July 12, 2006 Share Posted July 12, 2006 hi,this is how i use to connect with my database .if it works for you, then just go ahead and try..i too got lots of help from this forum.just create a file config.php[color=red]<?php$server = 'localhost';$user_name ='admin' //my user name$pw = 'ijk' //password to connect with MySQL$db = 'test' //the database$link = mysql_connect($server,$user,$pw)if(!$link){ echo 'No connection is found ';}else{ mysql_select_db($db,$link) or die('Connection found.No database found');}?>[/color]now in your php file, just add this line (process.php)[color=red]<?php include_once('config.php');?><?php $name=$_POST['name'];$email=$_POST['email'];$location=$_POST['location'];sql = "insert into table (name, email,location) values ('$name', '$email', '$location') "; $result=mysql_query($sql,$link) or die('Error in inserting values');if(!$result){ echo 'mysql_errno().mysql_error()';}?>[/color]This way, you can find the errors easily.anyother method if anyone knows that is better, pls tell to me.i hope this will help ucheerssai Quote Link to comment Share on other sites More sharing options...
greggustin Posted July 12, 2006 Author Share Posted July 12, 2006 [quote author=criticalsystems link=topic=100261.msg395561#msg395561 date=1152676914]you do not have to have a variable for ever column you can just specify the cols that you are going to give values for in the SQL statement.eg INSERT into table (col1,col2,col3) values (col1val,col2val,col3val);[/quote]this (your suggestion) worked Greattoo bad the original code I practiced with did not have the "columns id's" in them as did your examplenow each new submit has its 'ID' field auto increment in my table :) Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.