11 Replies - 699 Views - Last Post: 28 September 2012 - 02:25 PM Rate Topic: -----

#1 gskill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-September 12

Database problem.

Posted 27 September 2012 - 02:01 AM

Hello every1, and sorry to bother you guys again.

i have a problem with inserting data into my SQL database, the database is created and its working, the local server is running everything is ok but it still dosent work. Maybe i have the wariables wrong, or i dont know some typing error, but nothing apears.

Here is the code maybe some1 sees what i cant.
Maybe i should name my database and my table diferently ?
<?php
	$host="localhost";
	$username="root";
	$password="***********";
	$database="Information";
	$table="information";
	$nume='$_POST[name]';
	$email='$_POST[email]';
	$topic='$_POST[topic]';
	$descriere='$_POST[message]';
	$date=date("Y-m-d");
	$canty='$_POST[cant]';
	
	mysql_connect("$host","$username","$password") or die(mysql_error());
	echo "connected";
	mysql_select_db("$database") or die(mysql_error());
	echo "database found";
	
	$mysql="INSERT INTO $table(Nr.Reg,Nume,E-mail,Descriere,Date,Topic,Cantitate) VALUES ('0','$nume','$email','$descriere','$date','$topic','$canty')";
	
	if (!mysql_query($mysql))
	die (mysql_error()); 
	echo"Data inserted!";
	
	mysql_close();
	
?><head>
	<link href="Styles/StylesContacte.CSS" rel="stylesheet" type="text/css">
    <title>Contacte</title>
    <script type="text/javascript">
function MM_jumpMenu(targ,selObj,restore){ //v3.0
  eval(targ+".location='"+selObj.options[selObj.selectedIndex].value+"'");
  if (restore) selObj.selectedIndex=0;
}
    </script>
    <meta http-equiv="content-type" content="text/html;charset=utf-8" />
<hr />

	
    


<style type="text/css">
		form {
			width:480px;
			margin-left:auto;
			margin-right:auto;
			text-align:center;	
		}
		form ul {
			list-style-type:none;	
		}
		form ul li {
			margin:5px 5px;	
		}
		form label {
			display:block;
			border:#ccc;
			font-size:16px;	
		}
		form input , select{
			font-size:16px;
			width:100%;
			background-color:#FFFFFF;
		}
		
		#message textarea {
			border:solid 3px #ccc;
			background-color:#FFFFFF;
				
		}
	</style>
</head>
<body>
	<header>
    	<div id="banner">
        <img src="Pictures/Untitled-3.png" name="BannerPic" width="650" height="163" id="BannerPic">
        </div>
    
    </header>
<div id="menu">
  	<div id="menu_center">
   		   <ul>
           	<li><a href="Home.html">Home</a></li>
            <li><a href="Produse.html">Produse</a></li>	
           	<li><a href="Cumsefabrica.html">Cum se fabrica</a></li>
           	<li><a href="Mangalita.html">Mangalita</a></li>
            <li><a href="Contacte.html">Contacte </a></li>
       	  </ul>
    </div>
  </div>
	
  <div id="content">
	<div id="holder">
    	<div id="text1">
        	<textarea name="Description" cols="80" rows="23" readonly class="nosize-text" id="Desc" height="150" resize="none">

            </textarea>
            </div>
          <div id="mail">
          	<form action="?" method="post">
            	<ul>
                	<li>
                    	<label for="name">Numele:</label>
                        <input type="text" name="name" id="name" /> 
                	</li>
                    <li>
                    	<label for="email">Email:</label>
                        <input type="text" name="email" id="email" />
                    </li>
                    <li>
                    	<label for="cantitate">Cantitate(KG):</label>
                        <input type="text" name="cant" id="cant" />
                    </li>
                    <li>
                    	<label for="topic">Comanda:</label>
                        <select id="topic" name="topic">
                        	<option value="Ponies">Salam</option>
                            <option value="Mexicans">Sunca</option>
                            <option value="Weiner">Carnati</option>
                        </select>
                    </li>
                    <li>
                    	<label for="message">Tell me something</label>
                        <textarea id="message" name="message" cols="42" rows="7">   </textarea>
                    </li>
                    <li>
                    	<input type="submit" value="Trimite">
                    </li>
                </ul>
            </form>		
          </div>  
    </div> 
    <footer>
     <table border="0" align="center" cellpadding="10" cellspacing="5" id="table1">
  <tr>
    <td><a href="Home.html">•Home</a></td>
    <td><a href="Produse.html"> •Produse</a></td>
    <td><a href="Cumsefabrica.html"> •Cum se fabrica</a></td>
    <td><a href="Mangalita.html"> •Mangalita</a></td>
    <td><a href="Contacte.html"> •Contacte</a></td>
  </tr>
</table>
 
 </footer>
  </div>  
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Database problem.

#2 jasper199069  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 19-August 08

Re: Database problem.

Posted 27 September 2012 - 02:14 AM

Hi,

I have found some small things
when you are declaring your variables here:
$nume='$_POST[name]';
$email='$_POST[email]';
$topic='$_POST[topic]';
$descriere='$_POST[message]';
$date=date("Y-m-d");
$canty='$_POST[cant]';



you don't need the single quotes because then they will be recognized as a string instead of a variable.

Further your database connection seems fine to me.
but i think you can leave the quotes away there ;)

The rest of the php code seems in order.

I hop you can do something with my comment ;)

Kind regards,

Jasper
Was This Post Helpful? 1
  • +
  • -

#3 gskill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-September 12

Re: Database problem.

Posted 27 September 2012 - 02:20 AM

Removed it, but still nothing.
I don't know why it dosent send it to my database, i can add manualy data to my dabase, so that means the DB is setup corectly.

The only thing i dident realy understnad is that i pu Nr.Reg (register) to be incremeted, but i dont know how to declar it in the sending proces so i put "0" there, maybe thats the problem.
Was This Post Helpful? 0
  • +
  • -

#4 kai_itz me  Icon User is offline

  • D.I.C Head

Reputation: 28
  • View blog
  • Posts: 159
  • Joined: 03-August 12

Re: Database problem.

Posted 27 September 2012 - 03:03 AM

check out;

My link
Was This Post Helpful? 0
  • +
  • -

#5 jasper199069  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 33
  • Joined: 19-August 08

Re: Database problem.

Posted 27 September 2012 - 03:06 AM

That might be the problem (why did i forget about that).
When you are incrementing values it will always start with 1.

You can change the query to have the Nr.Reg removed like this
$mysql="INSERT INTO $table(Nume,E-mail,Descriere,Date,Topic,Cantitate) VALUES ('$nume','$email','$descriere','$date','$topic','$canty')";



This way if it is on auto_increment the database will increment the variable by itself.

Kind Regards,

Jasper
Was This Post Helpful? 0
  • +
  • -

#6 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6110
  • View blog
  • Posts: 23,670
  • Joined: 23-August 08

Re: Database problem.

Posted 27 September 2012 - 03:19 AM

Using deprecated MySQL functions, CHECK.
No sanitation of user inputs, leaving yourself wide open to SQL injection attacks, CHECK.
No printing of the assembled query when an error occurs on insert, CHECK.

All of these are newbie mistakes. The first two can be overcome by using PDO.

Assuming you are actually getting an error on insert (you didn't bother to tell us, and if you are, you didn't tell us the error), the third is just common sense. You are assembling a query from data. If they query is failing, then you should print the assembled query so you can see what's actually being passed to the database.
Was This Post Helpful? 0
  • +
  • -

#7 gskill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-September 12

Re: Database problem.

Posted 27 September 2012 - 03:27 AM

No i'm not getting any error messages, it just dosent write it in the database thats all, i dont know what could the problem be, i fixed everything that the people suggested to me but still nothing.

it just simply dosent do anything, i have no syntax error.
I'ts probably some stupid little mistake that i made and i cant notice it.
Was This Post Helpful? 0
  • +
  • -

#8 RudiVisser  Icon User is offline

  • .. does not guess solutions
  • member icon

Reputation: 1004
  • View blog
  • Posts: 3,562
  • Joined: 05-June 09

Re: Database problem.

Posted 27 September 2012 - 05:19 AM

Does your code even run?

If you don't see Data inserted! output above your page then PHP isn't executing, check the page source.
Was This Post Helpful? 0
  • +
  • -

#9 xYassineHoussni  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 9
  • Joined: 21-September 12

Re: Database problem.

Posted 27 September 2012 - 03:11 PM

All of your pages are .html pages, are you sure the current page is a .php file?
Was This Post Helpful? 0
  • +
  • -

#10 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1623
  • View blog
  • Posts: 5,200
  • Joined: 25-September 09

Re: Database problem.

Posted 27 September 2012 - 03:22 PM

Is the name Date valid for a field name in the database? it appears to be a keyword so I wonder if it's throwing off the sql statement. Just not sure why no error is thrown.
Was This Post Helpful? 1
  • +
  • -

#11 gskill  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 26-September 12

Re: Database problem.

Posted 27 September 2012 - 11:09 PM

Yes my file is called contact.php so i'm sure it is a php.

And no it dosent say Data inserted at the top of the page, i think that means that my PHP is not executed. Why could that be ?
Was This Post Helpful? 0
  • +
  • -

#12 e_i_pi  Icon User is offline

  • = -1
  • member icon

Reputation: 801
  • View blog
  • Posts: 1,700
  • Joined: 30-January 09

Re: Database problem.

Posted 28 September 2012 - 02:25 PM

To futher the point CharlieMay has made, I would suggest backticking all of your column names, as you have some unconventional names in there. I would question not only Date but also Nr.Reg and E-Mail. I would try this instad:
INSERT INTO $table(`Nr.Reg`,`Nume`,`E-mail`,`Descriere`,`Date`,`Topic`,`Cantitate`) VALUES ('0','$nume','$email','$descriere','$date','$topic','$canty')


Also, as JackOfAllTrades said, use PDOs. When you use PDOs you don't have to do type-casting or quoting, meaning you can avoid issues caused by placing quotes erroneously around numeric fields, or vice versa.

If the message "data inserted" isn't printed out, what is there in it's place? I would imagine an error would have printed out. If not, you will have to check your error settings, as errors may be getting ignored, or logged to a file.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1