Reputation: 47
I have a simple database with first, last, age and bio fields. It is working fine but I would like to change the first name field so when user add firstname, it shows hyperlink on front-end.
URL = http://inamelookuptest.com/info/default.aspx?first_name=First_Name
So what I am trying to accomplish is when someone adds their first name under first field e.g, Scot. When it adds it to DB, it shows Scot but as a hyperlink so when they click, it goes to this URL:
URL = http://inamelookuptest.com/info/default.aspx?first_name=Scot
I did google search and got some idea but it was breaking my code. Here is my code:
Add.php
<?php
require 'conx.php';
$data = json_decode(file_get_contents("php://input"));
$fn = $data->fn;
$ln = $data->ln;
$age = $data->age;
$bio = $data->bio;
$result = $db->query("INSERT INTO users(firstname,lastname,age,bio) values('$fn','$ln','$age','$bio')");
I tried this but it broke adding new items functionality:
$result = $db->query("INSERT INTO users(firstname,lastname,age,bio) values('<a href="http://inamelookuptest.com/info/default.aspx?first_name= + $fn">'$fn'</a>','$ln','$age','$bio')");
Edit:
<tbody ng-init="getall()">
<tr ng-repeat="d in names | filter:search">
<td>{{ d.uid }}</td>
<td>{{ d.Firstname }}</td>
<td>{{ d.Lastname }}</td>
<td>{{ d.Age }}</td>
<td>{{ d.Bio }}</td>
I added link to firstname as below but I am getting unexpected url:
<td> <a href="http://inamelookuptest.com/info/default.aspx?first_name='{{ d.Firstname }}'"> {{ d.Firstname }} </a></td>
All items are now hyperlinked but when I hover on Scot I see this:
http://inamelookuptest.com/info/default.aspx?first_name=%27scot%27
Edit 2:
I figured it out:
<td> <a href="http://inamelookuptest.com/info/default.aspx?first_name={{ d.Firstname }}"> {{ d.Firstname }} </a></td>
Thanks all!!
Upvotes: 0
Views: 65
Reputation: 1447
If the URL is always the same then you dont need to add it in MySQL.
Anyway try this code:
<?php
require 'conx.php';
$data = json_decode(file_get_contents("php://input"));
$fn = $data->fn;
$ln = $data->ln;
$age = $data->age;
$bio = $data->bio;
$urlfn = '<a href="http://inamelookuptest.com/info/default.aspx?first_name='.$fn.'">'.$fn.'</a>';
$result = $db->query("INSERT INTO users(firstname,lastname,age,bio) values('$urlfn','$ln','$age','$bio')");
Actually what you should do is to add URL after when you get results from MySQL. This way you will not need to save same URL over and over again in MySQL.
So first fetch results from MySQL like:
$fn = $data->fn; // Output is Scot
Then add your URL via PHP so when echo/output $fn like:
echo '<a href="http://inamelookuptest.com/info/default.aspx?first_name='.$fn.'">'.$fn.'</a>';
Upvotes: 1
Reputation: 344
The problem is the double quotes around for your href
attribute is breaking your insert statement. You will need to escape these double quoteswith a backslash \"
as below:
$result = $db->query("INSERT INTO users(firstname,lastname,age,bio) values('<a href=\"http://inamelookuptest.com/info/default.aspx?first_name= + $fn\">'$fn'</a>','$ln','$age','$bio')");
Upvotes: 0