473,664 Members | 2,967 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

INSERT INTO statement won't take variables as values!

3 New Member
When i try to use post variables with php and mysql i can't get the insert into statement to accept varibles as values. If i use 'test' instead of $test it does work. I suspect it is something to do with the javascript im using but i can print the correct values so why am i unable to use them to enter data to a database? sorry i am new to all this

here is the updatedata.php file
Expand|Select|Wrap|Line Numbers
  1. <?php 
  2.  
  3. session_start(); 
  4.  
  5. $host="localhost"; // Host name 
  6. $username="root"; // Mysql username 
  7. $password="xxxxxxx"; // Mysql password 
  8. $db_name="test"; // Database name 
  9. $tbl_name="Student"; // Table name 
  10.  
  11. // Connect to server and select databse.
  12. mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
  13. mysql_select_db("$db_name")or die("cannot select DB");
  14.  
  15. // username and password sent from signup form 
  16. $quizID=$_POST['quizID']; 
  17. $score=$_POST['Score'];
  18.  
  19. $sql="INSERT INTO Marks (field1, filed2) VALUES ('10', '1')";
  20. mysql_query($sql);
  21.  
  22. echo $score;
  23. echo $quizID;
  24.  
  25. session_register("quizID");
  26. session_register("Score"); 
  27.  
  28. //echo $score;
  29. //echo $quizID;
  30. //header("location:welcome.xml");
  31. ?> 
  32.  
  33. when i echo the values of the score and quizID varibles i get the correct data printed to screen but this data isnt entered into the database.
  34.  
  35. unlike my other insert into statment in another php file which works using a submit button in an html file, this time the php file is triggered via a hyperlink.
  36.  
  37. This is the javascript function that is called on clicking on the hyperlink:
  38.  
  39. function setItemDetails()
  40. {
  41.  
  42. alert("test complete");
  43.  
  44. document.myform.quizID.value = 1;
  45. document.myform.Score.value = correctAnswers;
  46. document.myform.submit();
  47. }
  48.  
  49.  
  50. This is where the hyperlink is
  51.  
  52.  
  53. <body bgcolor=gray>
  54.  
  55. <form name="myform" action="updatedata.php" method="post">
  56.  
  57. <input type="hidden" name="quizID" value="1" />
  58. <input type="hidden" name="Score" value="correctAnswers" />
  59.  
  60. <a href="javascript:setItemDetails()">Finish</a>
  61. </form>
  62.  
  63.  
  64. </body>
  65. </html>
  66.  
  67. The variables are declared globally 
  68.  
  69.  
  70. // Define global variables and arrays
  71. var questionIndex    = 0;
  72. var checker         = true;
  73. var keeper         = new Array();
  74. var correctAnswers     = 0;
  75. var quizID              = 1;
  76.  
Any idea why this isnt working?
Feb 28 '08 #1
2 3197
ronverdonk
4,258 Recognized Expert Specialist
Showing the code, the way you did, does not tempt many people to go through it.

First read the Posting Guidelines and enclose any code shown within the appropriate code tags.

moderator
Feb 28 '08 #2
ronverdonk
4,258 Recognized Expert Specialist
I sure hope that field 'filed2' is a typo, otherwise you have the error.

It would be a lot better when MySQL instructions were tested for the completion code. That is the only way of catching any spelling or syntax errors. So change your statement like this :[php]mysql_query($sq l)
or die("INSERT error: ".mysql_error() );[/php]and the error (when in the insert) will be shown.

Ronald
Feb 28 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

10
3223
by: shank | last post by:
I have a recordset that contains multiple records of product a user is purchasing. For clarity, I converted the recordset fields to variables. I need to take that entire recordset and insert it into another table on a remote server. The below code only inserts 1 record. How do I change the code to get all records inserted? thanks! <% Dim DataConn2 Set DataConn2 = Server.CreateObject("ADODB.Connection")
1
8734
by: PT | last post by:
I got a problem. And thats..... First of all, I got these three tables. ------------------- ------------------ ---------------------- tblPerson tblPersonSoftware tblSoftware ------------------- ------------------ ---------------------- PID PName PID* SID* SID SWName --- ----- --- --- --- ------ 1 Thomas 1 1 ...
2
17100
by: Ford Desperado | last post by:
I've been reading the docs and playing around, but I'm still not getting the difference. For instance, create table a(i int check(i>0)) create table a_src(i int) go create unique index ai on a(i) with IGNORE_DUP_KEY go insert into a_src values(1) insert into a_src values(1)
2
35836
by: Nothing | last post by:
I have a SQL statement that is not working. It reads as follows: sql = "INSERT INTO tblPaymentInformation (, , , , ) VALUES ('aucdte', 'bidnum', 'paytyp', 'paynum', payamt)" The variables for the VALUES section are assigned before this sql statement. The problem occures with the payamt varable. It is a currency
1
1884
by: Nothing | last post by:
I am trying to use the sql INSERT INTO statement to add a new record. This is the SQL statement: sql = "INSERT INTO tblpatients(bbid, patientname, orderdate, ordertime, mrnumber, sex, age, drawnby, drawnbytime, doctor, dob, location, type-r-h, donortype-r-h, componet, antibody, unitnum, expdate, coombs, crossmatch, rhogam, unitnumber2, expdate2, fetal, commanet) VALUES ('bid', 'ptname', 'orddte', 'ordtime', 'mrnum', 'ptsex', 'ptage',...
11
16216
by: sm | last post by:
Hi All, Can anybody give me the syntax to insert a record into SQL server through VB code using variables? The following statement is failing! sInsertQuery = "INSERT INTO TestTab (Col1, Col2, Col3, Col4) Values(" & _ str1 & "," & dt1 & "," & str2 & "," & str3 & ")" where str1 = "col1"
3
1479
by: Blarneystone | last post by:
Ok, I've got a simple access 97 db. named S_tracking.mdb It has two tables 1- Jobs 2- Employees I've set up the references: Imports System.Data.OleDb Imports System.IO Imports System.data
2
30149
by: franc sutherland | last post by:
Hello, I am using Access 2003. Is it possible to use string variables in the INSERT INTO statement? I am using the INSERT INTO statement to add a long list of contacts to a group by looping through the recordset (based on a linked spreadsheet, 'tbl_group_import') and using the INSERT INTO statement on each loop. I am using the rst.Fields.Item("FieldName") method to
1
2512
by: Maklar60 | last post by:
I am attempting to execute an INSERT statement on my page but continually get the following error: Microsoft OLE DB Provider for ODBC Drivers error '80040e14' Incorrect syntax near '<'. /int_code04/myNMLC/insertNewTrackRecord.asp, line 97 I've tested the INSERT stmt both within SQL Server and as a string literal within the page's code with hardcoded values to ensure that the statement works, which is does. But when I assign this...
0
8437
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8861
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4185
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4351
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2003
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1759
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.