Jump to content

PHP, MySQL and Wordpress - Issue loading data into wordpress page


drew4663

Recommended Posts

I have a php tutorial that I followed for creating, inserting, selecting and updating a MySQL database with php.  Everything works fine so I wanted to put it into Wordpress.  I took the code and placed it into the wordpress page and everything worked just fine except the update function. 

 

Here is the tutorial I used. http://www.phpsimple.net/mysql_insert_record.html  It is also the part I am having trouble with.  I am able to create a record and I am also able to select a record but when I choose "update" the form doesn't load the data.  It will outside the website but not inside wordpress.  I am hoping this is not vague but because of my inexperience I am not sure what else to say.  I will be more than happy to provide any other information you need.

Link to comment
Share on other sites

First Page used for connecting to the database and creating a record and writing to it.  This form works in wordpress.  Below is the code I use.

 

<!-- set this form to POST method and target this form to insert.php -->
<style type="text/css">
<!--
.wrapper {
width: 700px;
background-image: url(http://ecaunderground.com/wp-content/images/yootheme/logo.png);
background-position: center top;
background-repeat: no-repeat;
background-color: #000;
color: #FFF;
}
.name {
height: 30px;
width: 700px;
;
color: #FFF;
padding-top: 10px;
padding-right: 0px;
padding-bottom: 10px;
padding-left: 10px;
border: thin solid #CCC;
}
.start {
width: 700px;
color: #FFF;
padding-top: 10px;
padding-right: 0px;
padding-bottom: 10px;
padding-left: 10px;
border: thin solid #CCC;
}
.weekly_pounds_lost {
width: 700px;
color: #FFF;
padding-top: 10px;
padding-right: 0px;
padding-bottom: 10px;
padding-left: 10px;
text-align: left;
border: thin solid #CCC;
}
.weekly_inches_start {
width: 700px;
color: #FFF;
padding-top: 10px;
padding-right: 0px;
padding-bottom: 10px;
padding-left: 10px;
text-align: left;
border: thin solid #CCC;
}
.weekly_inches_lost {
width: 700px;
color: #FFF;
padding-top: 10px;
padding-right: 0px;
padding-bottom: 10px;
padding-left: 10px;
text-align: left;
}
.other_info {
width: 700px;
color: #FFF;
padding-top: 10px;
padding-right: 0px;
padding-bottom: 10px;
padding-left: 10px;
text-align: left;
border: thin solid #CCC;
}
.space {
height: 20px;
width: 700px;
}
body {
background-color: #000;
}
-->
</style>
<!--///////////////////////////////////////////////////////////////////////wrapper class////////////////////////////////////////////////////-->
<div class="wrapper">
<form id="form1" name="form1" method="post" action="insert.php">
<br />
<br />
<br />
<br />
Name:
<!--///////////////////////////////////////////////////////////////////////Name class////////////////////////////////////////////////////-->
<div class="name">
  First :
  <input name="FirstName" type="text" id="FirstName" />
  
  Last :
  <input name="LastName" type="text" id="LastName" />
  
  Age:
  <input name="Age" type="text" id="Age" size="2" maxlength="2" />
  
  Height:
  <input name="Height" type="text" id="Height" size="2" maxlength="6" />
  
  BMI:
  <input name="BMI" type="text" id="BMI" size="1" maxlength="2" />
  </div>
  <div class="space"> </div>
<!--///////////////////////////////////////////////////////////////////////Start class////////////////////////////////////////////////////-->
Start Info:
<div class="start">
  Start Date:
  <input name="StartDate" type="text" id="StartDate" size="5" maxlength="10" />
  Start Weight:
  <input name="StartWeight" type="text" id="StartWeight" size="2" maxlength="3" />
  Goal Date:
  <input name="GoalDate" type="text" id="GoalDate" size="5" maxlength="10" />
  
  Weigh-in Day:
  <input name="WeighInDay" type="text" id="WeighInDay" size="5" maxlength="10" />
  
  Goal Weight:
  <input name="GoalWeight" type="text" id="GoalWeight" size="2" maxlength="3" />
</div>
<div class="space"> </div>
<!--///////////////////////////////////////////////////////////////////////weekly_pounds_lost class////////////////////////////////////////////////////-->
Pounds Lost:
<div class="weekly_pounds_lost">
  Wk 1:
  <input name="Week1" type="text" id="Week1" size="1" maxlength="2" />
  
  
    Wk 2:
<input name="Week2" type="text" id="Week2" size="1" maxlength="2" />
  
  
    Wk 3:
<input name="Week3" type="text" id="Week3" size="1" maxlength="2" />
  
  
    Wk 4:
<input name="Week4" type="text" id="Week4" size="1" maxlength="2" />
  
  
  Wk 5:
  <input name="Week5" type="text" id="Week5" size="1" maxlength="2" />
  
  
  Wk 6:
  <input name="Week6" type="text" id="Week6" size="1" maxlength="2" />
  
    Wk 7:
  <input name="Week7" type="text" id="Week7" size="1" maxlength="2" />
  
  Wk 8:
  <input name="Week8" type="text" id="Week8" size="1" maxlength="2" />
  <br />
  <br />
  Wk 9:
  <input name="Week9" type="text" id="Week9" size="1" maxlength="2" />
  
  Wk 10:
  <input name="Week10" type="text" id="Week10" size="1" maxlength="2" />
  
  Wk 11:
  <input name="Week11" type="text" id="Week11" size="1" maxlength="2" />
  
  Wk 12:
  <input name="Week12" type="text" id="Week12" size="1" maxlength="2" />
</div>
  <div class="space"> </div>
  <!--///////////////////////////////////////////////////////////////////////inches_start class////////////////////////////////////////////////////-->
  Inches at Start:
  <div class="weekly_inches_start">
  Neck:
  <input name="NeckStart" type="text" id="NeckStart" size="2" maxlength="2" />
  
  Chest:
  <input name="ChestStart" type="text" id="ChestStart" size="2" maxlength="2" />
  
  Stomach:
  <input name="StomachStart" type="text" id="StomachStart" size="2" maxlength="2" />
Thighs:
<input name="ThighsStart" type="text" id="ThighsStart" size="2" maxlength="2" />
  
  Arms:
  <input name="ArmsStart" type="text" id="ArmsStart" size="2" maxlength="2" />
  
  Calves:
  <input name="CalvesStart" type="text" id="CalvesStart" size="2" maxlength="2" />
  
  Hips:
  <input name="HipsStart" type="text" id="HipsStart" size="2" maxlength="2" />
  </div>
  
  <div class="space"> </div>
  <!--///////////////////////////////////////////////////////////////////////inches_start class////////////////////////////////////////////////////-->
  Inches Current:
  <div class="weekly_inches_start">
  
  Neck:
  <input name="NeckCurrent" type="text" id="NeckCurrent" size="2" maxlength="2" />
  
   Chest:
<input name="ChestCurrent" type="text" id="ChestCurrent" size="2" maxlength="2" />
  
  Stomach:
  <input name="StomachCurrent" type="text" id="StomachCurrent" size="2" maxlength="2" />
  
  Thighs:
  <input name="ThighsCurrent" type="text" id="ThighsCurrent" size="2" maxlength="2" />
  
  Chest:
  <input name="ChestCurrent" type="text" id="ChestCurrent" size="2" maxlength="2" />
  
  Stomach:
  <input name="StomachCurrent" type="text" id="StomachCurrent" size="2" maxlength="2" />
  
  Thighs:
  <input name="ThighsCurrent" type="text" id="ThighsCurrent" size="2" maxlength="2" />
  <br />
  <br />
  Arms:
  <input name="ArmsCurrent" type="text" id="ArmsCurrent" size="2" maxlength="2" />
  
  Calves:
  <input name="CalvesCurrent" type="text" id="CalvesCurrent" size="2" maxlength="2" />
        Hips:
<input name="HipsCurrent" type="text" id="HipsCurrent" size="2" maxlength="2" />
  </div>
<div class="space"> </div>
<!--///////////////////////////////////////////////////////////////////////other_info class////////////////////////////////////////////////////-->
Other Info:
<div class="other_info">

  Visalus Customer:
  <input name="VisalusCustomer" type="text" id="VisalusCustomer" size="3" maxlength="3" />
  
  Visalus Distributor:
  <input name="VisalusDistributor" type="text" id="VisalusDistributor" size="3" maxlength="3" />
  
  Challenge:
  <input name="Challenge" type="text" id="Challenge" size="3" maxlength="3" />
  </div>
<p>
  <input type="submit" name="Submit" value="Submit" />
</p>
</form> 
</div>

 

This is the code that I use to insert the data into the database.  It's called by the first page above.

<?

// Connect database.
include("connectdb.php");

// Get values from form.
$FirstName=$_POST['FirstName'];
$LastName=$_POST['LastName'];
$StartDate=$_POST['StartDate'];
$StartWeight=$_POST['StartWeight'];
$WeighInDay=$_POST['WeighInDay'];
$GoalDate=$_POST['GoalDate'];
$GoalWeight=$_POST['GoalWeight'];
$Week1=$_POST['Week1'];
$Week2=$_POST['Week2'];
$Week3=$_POST['Week3'];
$Week4=$_POST['Week4'];
$Week5=$_POST['Week5'];
$Week6=$_POST['Week6'];
$Week7=$_POST['Week7'];
$Week8=$_POST['Week8'];
$Week9=$_POST['Week9'];
$Week10=$_POST['Week10'];
$Week11=$_POST['Week11'];
$Week12=$_POST['Week12'];
$NeckStart=$_POST['NeckStart'];
$ChestStart=$_POST['ChestStart'];
$StomachStart=$_POST['StomachStart'];
$ThighsStart=$_POST['ThighsStart'];
$ArmsStart=$_POST['ArmsStart'];
$CalvesStart=$_POST['CalvesStart'];
$HipsStart=$_POST['HipsStart'];
$NeckCurrent=$_POST['NeckCurrent'];
$ChestCurrent=$_POST['ChestCurrent'];
$StomachCurrent=$_POST['StomachCurrent'];
$ThighsCurrent=$_POST['ThighsCurrent'];
$ArmsCurrent=$_POST['ArmsCurrent'];
$CalvesCurrent=$_POST['CalvesCurrent'];
$HipsCurrent=$_POST['HipsCurrent'];
$VisalusCustomer=$_POST['VisalusCustomer'];
$VisalusDistributor=$_POST['VisalusDistributor'];
$Challenge=$_POST['Challenge'];
$Age=$_POST['Age'];
$Height=$_POST['Height'];
$BMI=$_POST['BMI'];



// Insert all parameters into database.
// The id field is auto increment. You don't have to insert any value
mysql_query("insert into memberstats(FirstName, LastName, StartDate, StartWeight, WeighInDay, GoalDate, GoalWeight, Week1, Week2, Week3, Week4, Week5, Week6, Week7, Week8, Week9, Week10, Week11, Week12, NeckStart, ChestStart, StomachStart, ThighsStart, ArmsStart, CalvesStart, HipsStart, NeckCurrent, ChestCurrent, StomachCurrent, ThighsCurrent, ArmsCurrent, CalvesCurrent, HipsCurrent, VisalusCustomer, VisalusDistributor, Challenge, Age, Height, BMI) values('$FirstName', '$LastName', '$StartDate', '$StartWeight', '$WeighInDay', '$GoalDate', '$GoalWeight', '$Week1', '$Week2', '$Week3', '$Week4', '$Week5', '$Week6', '$Week7', '$Week8', '$Week9', '$Week10', '$Week11', '$Week12', '$NeckStart', '$ChestStart', '$StomachStart', '$ThighsStart', '$ArmsStart', '$CalvesStart', '$HipsStart', '$NeckCurrent', '$ChestCurrent', '$StomachCurrent', '$ThighsCurrent', '$ArmsCurrent', '$CalvesCurrent', '$HipsCurrent', '$VisalusCustomer', '$VisalusDistributor', '$Challenge', '$Age', '$Height', '$BMI')");

// Close database connection
mysql_close();
header("location:http://www.ecaunderground.com/");

?>

 

This code gives me the opportunity to load a record and display text of a particular field(s).  It gives a link for updating and snags the id putting it into the url.  This works inside wordpress as well.

<?

// Connect database
include("connectdb.php");

// Get all records in all columns from table and put it in $result.
$result=mysql_query("select * from memberstats");

/*Split records in $result by table rows and put them in $row.
Make it looping by while statement. */
while($row=mysql_fetch_assoc($result)){

// Output
echo "id : $row[id] <br/>";
echo "FirstName : $row[FirstName] <br/>";
echo "LastName : $row[LastName] <br/>";
echo "GoalWeight : $row[GoalWeight] <br/>";

// Add a link with a parameter(id) and it's value.
echo '<a href="http://ecaunderground.com/update.php?id='.$row['id'].'">Update</a>';

}

mysql_close();
?> 

 

After clicking the link the update.php is loaded and displays the html form but doesn't load any data.  It's weird that all of the other php is fine but not this.  I'm not sure if it has something to do with the location  or not but here is the code.

<?
// START PHP CODES. THIS PART MUST ON THE TOP OF THIS PAGE.

// Connect database.
include("connectdb.php");

// ***** This part will process when you Click on "Submit" button *****
// Check, if you clicked "Submit" button
if($_POST['Submit']){

// Get parameters from form.
$id=$_POST['id'];
$FirstName=$_POST['FirstName'];
$LastName=$_POST['LastName'];
$GoalWeight=$_POST['GoalWeight'];

// Do update statement.
mysql_query("update memberstats set FirstName='$FirstName', LastName='$LastName', GoalWeight='$GoalWeight' where id='$id'");

// Re-direct this page to select.php.
header("location:select.php");
exit;
}
// ************* End update part *************

// *** Select data to show on text fields in form. ***

// Get id parameter (GET method) from select.php
$id=$_GET['id'];

// Get records in all columns from table where column id equal in $id and put it in $result.
$result=mysql_query("select * from memberstats where id='$id'");

// Split records in $result by table rows and put them in $row.
$row=mysql_fetch_assoc($result);

// Close database connection.
mysql_close();

?>

<!-- END OF PHP CODES AND START HTML TAGS -->

<html>
<body>

<form id="form1" name="form1" method="post" action="<? echo $PHP_SELF; ?>">
<p>
ID :
<input name="id" type="text" id="id" value="<? echo $row['id']; ?>"/>
<br />
First Name :
<input name="FirstName" type="text" id="FirstName" value="<? echo $row['FirstName']; ?>"/>
<br />
Last Name :
<input name="LastName" type="text" id="LastName" value="<? echo $row['LastName']; ?>"/>
<br />
Goal Weight :
<input name="GoalWeight" type="text" id="GoalWeight" value="<? echo $row['GoalWeight']; ?>"/>
</p>
<p>
<input type="submit" name="Submit" value="Submit" />
</p>
</form>
</body>
</html>

 

This is all the code involved.  Like I said before, it works outside of wordpress but the update does not.  I think it has something to do with the url but don't know how to troubleshoot it.  Let me know if you need anything else.  As of right now the wordpress pages I have this code in are private pages but can temporarily allow them to be seen if it will help.  Thanks.

 

Andrew

Link to comment
Share on other sites

Ok, firstly I said relevant code, not the entire program. You need to narrow your issue down somewhat if you expect help.

 

How exactly does this code relate to Wordpress? Are you trying to extend Wordpress somehow or are you just putting this code within the same site? Wordpress can use a .htaccess file to make pretty urls, if this is indeed the case, we need to see this .htaccess file.

Link to comment
Share on other sites

First, I don't give enough information.  Second, I give too much information.  You asked for relevant code.  I explained already I am inexperienced so obviously what is relevant to me is going to be different to what is relevant to you.  No offense but I did narrow the problem down.  I just thought it might be "relevant" to post the processes that I was using from beginning to end.  If you would take the time to read you would notice that I said all of the code works except the last which is only a few lines of code and I even explained the problem I was having.  So if you are unable to help me move on please.  Having to deal with you when I am someone looking for help and in need is adding to the stress of my problem so I am going to move on to a more reputable site.  Thanks for the attitude admin.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.