2

I am new to PHP in Ubuntu 13.10. But I am pretty much able to handle Ubuntu. However my question is that I can't add any data to phpmyadmin through a PHP code, though my code is perfect. Because I have the same code in WAMP server, and it worked perfectly. But in Ubuntu I just can't add any data to the database. Below seen is the the code of php file;

<?php
$db_name="mydb";
$table_name="student";
$con=@mysql_connect("localhost",root);
$db=@mysql_select_db($db_name,$con);
$sql="insert into $table_name (name,course,mobile,address)"."values ('$_POST[n1]','$_POST[n2]',$_POST[n3],'$_POST[n4]')";
$r=@mysql_query($sql,$con);
echo "----Insert successfull----,<br><br><hr/>";
echo "<a href=index.html>Back</a>";
?>

Please tell me whether I am wrong or is there is any bug/problem in phpmyadmin.

AzkerM
  • 10,270
  • 6
  • 32
  • 51
user259410
  • 21
  • 1
  • 4
  • I think you need a space in your sql statement: address)".' '."values – Parto Mar 18 '14 at 05:47
  • are you sure? but it works on WAMP as i told before – user259410 Mar 18 '14 at 05:55
  • still not working and one more question,when i try to open my php file,it does not open in browser it's showing "what should Firefox do with this file 1.open with 2.save file and 3.do this automatically for files like this from now on". how can i fix this? – user259410 Mar 18 '14 at 06:01
  • It seems you don't have LAMP server installed or you're not running your php files from your www folder. That's why Firefox is showing you that error and your PHP code isn't working. Look at this question on how to install and configure your LAMP server. – Parto Mar 18 '14 at 07:36
  • i have already install the LAMP server by command.and my localhost is working perfect.and the file is under /var/www/student_db .so it should work perfectly – user259410 Mar 18 '14 at 08:10
  • Try restarting your apache server 'sudo service apache2 restart' and check your /var/www/ folder permissions – Parto Mar 18 '14 at 08:16
  • 1
    This will be more useful on Stack Overflow. –  Mar 18 '14 at 08:36
  • @Avatar Parto though i found my fault on my 2nd problem, actually i was trying to execute my file directly. now i have realise that i have to execute the file by typing localhost.anyway thanks.but still looking for my first problem – user259410 Mar 18 '14 at 08:47
  • It would be nice to report also what your error logs are saying when you execute your PHP. /var/log/apache2/error.log could be full of interesting information. But remove the @ in front of each of your mysql command to be sure you get the error is any. – Benoit Mar 18 '14 at 08:49
  • Just insert that space and try again via localhost and not directly. – Parto Mar 18 '14 at 09:30

4 Answers4

0

I'd try to use the mysql_pconnect(host,user,password) function to connect to the server. About your second problem it sounds to me the php library for apache is not installed or configured correctly. open synaptic and look for any apache-php missing library and as minimum try to (re)install libapache2-mod-php5

Hope it'll help

G.

Januz
  • 74
  • 5
  • as i have already told this code works perfectly on wamp and i have connect the mysql by $con=@mysql_connect("localhost",root);$r=@mysql_query($sql,$con); command. yes i have latest LAMP server installed link – user259410 Mar 18 '14 at 08:17
0

is $_POST[n2] value integer.if not then try to use $_POST['n2']

sohel4r
  • 464
0

in deed that was my fault,when i was trying to input data in the database,in mysql table i typed "First Name","Last Name" but after a certain time i thought the database may not be take spaces in variable name and i type "First_Name","Last_name" and it worked.and i was trying to execute the php file directly as i told before.now i have solved it the file have to execute by localhost. anyways thank you guys,

user259410
  • 21
  • 1
  • 4
  • Please accept your own answer as the correct one so that this question doesn't appear in the list of unanswered questions. Thanks. – papukaija Mar 18 '14 at 14:10
0

Try Changing your SQL query to this:

$sql = "insert into $table_name ('First Name','Last Name','Course','Address','Mobile','Date of Birth','Email') values ('".$_POST['n2']."','".$_POST['n3']."','".$_POST['n4']."','".$_POST['n5']."','".$_POST['n6']."','".$_POST['n7']."','".$_POST['n8']."')";
Parto
  • 15,325
  • 24
  • 86
  • 117